RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Android Mobility: Open Source Hits the Road : Page 3

Learn to leverage Android's powerful APIs to rapidly create sophisticated applications for media, data storage, and networking.

Stick Around
The application won't be very useful if it only saves a single picture. You need to create a database that will keep track of all the pictures your users will take, as well as their associated information—things like their creation time and captions.

Google's Notepad tutorial application includes a sample wrapper, which wraps around a SQLite database called NotesDbAdapter. This is a good foundation for your application's database needs: it provides the power of SQL with a friendly API. The code shown in Listing 3 affords the same convenience.

With this adapter in hand, you can easily save a record of the picture:

CameraDbAdapter adapter = new CameraDbAdapter(Camera.this);
adapter.createImage(filename, "Enter a caption.", System.currentTimeMillis());
Photo Album
You now have an application that saves and stores pictures on the device. To make it more useful, what about providing a way to review the pictures that have been taken?

To display the items as a simple list, you can extend ListActivity and have it manage your UI. All you need is to define what each line item should look like. This happens with a single element in /res/layout/camera_item.xml:

<TextView android:id="@+id/albumItemCaption"  
The corresponding Album Activity is simple as well. With the database keeping track of your content and the ListActivity handling your interface, all you need is a little glue to tie those pieces together. That glue comes in the form of an Adapter. For a basic UI, the SimpleCursorAdapter will do the trick:

CameraDbAdapter adapter = new CameraDbAdapter(this);
SimpleCursorAdapter labels = new SimpleCursorAdapter
    (this, R.layout.camera_item, adapter.fetchAllImages(),
     new String[]{CameraDbAdapter.KEY_CAPTION},
     new int[]{R.id.albumItemCaption});
The parameters in the SimpleCursorAdapter bind the UI layout elements to the appropriate Cursor data. That's all there is to it! You could further customize the list by modifying the albumItemCaption XML element.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date