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


Android Mobility: Open Source Hits the Road : Page 4

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

Linked In
How do you move from the initial Camera screen to the new Album screen? Any time you think about doing a screen transition in Android, your first thought should be Intent. Intents can be very powerful abstract mechanisms for tying together disparate pieces of functionality. Within an application, however, they tend to be very simple: you just specify where to go.

The Camera should go to the Album after taking a picture or clicking the Album button. A simple helper function will contain all that is needed:

private void advance()
    Intent i = new Intent(this, Album.class);
Call this function from within the onClickListener for the two Camera buttons. Listing 4 shows the completed Camera class.

One more step is needed to allow this transition. Although you've created AndroidManifest.xml:

<activity android:name=".Album" android:label="@string/app_name">
This may look a little sparse compared to other <activity> definitions, but it is actually quite common. Most Activities you'll write aren't meant to be accessed from outside your application. Thus, they do not need the intent-filters of your entry point.

What's My Name?
It's a little boring to give every picture the same caption. A simple editor will let you review the picture and add a caption. Listing 5 shows the layout for a new Editor Activity. The XML for this screen adds two new types of elements: editable text and images.

While the specific items may be different, the strategy for dealing with these new elements in the Activity is the same as for Camera:

  1. Set the content layout to the XML file.
  2. Retrieve items of interest by ID.
  3. Modify items and attach listeners to implement specialized behavior.
The complete Editor in Listing 6 is more complex than the previous two classes, so some more details may be helpful. The Editor needs some way of knowing which specific image is being edited. The standard way to do this is through an Intent Extra. An "extra" is a simple key-value pair; in this case, a long value that uniquely identifies the database row ID for this image.

Displaying the image is compact but complex:

image.setImageBitmap( BitmapFactory.decodeFile(getFilesDir() + "/" +
Breaking it apart:
  • Look up the database column that holds the filename.
  • Retrieve the actual filename String value.
  • Construct the fully-qualified filepath.
  • Pass this filepath to the BitmapFactory (a utility class that generates Bitmaps).
  • Set the loaded bitmap on the Image.
Setting the current caption on the EditText view is simple in comparison:

Figure 2. Camera Editor: Viewing and editing a saved picture.

mCaption.setText(mCursor, mCursor.getColumnIndex(CameraDbAdapter.KEY_CAPTION));
Finally, note that the caption is updated in the onPause() method. This is a common idiom known as "Edit in place". Rather than burdening the user with special menus and reminders to save, all edits are seamlessly performed as the user moves in and out of the Activity. It may feel unusual if you are used to more conventional behavior for saving, but it is very well suited to mobile devices.

Before seeing the Editor in action (see Figure 2), remember to add it to AndroidManifest.xml and add an appropriate Intent to Album. To tell the Editor which Image to retrieve, be sure to include the extra value for the Row ID. Listing 7 contains the now-complete code for Album.

By this point, you should be able to navigate the program and view stored pictures.

Author's Note: If you copied code from the Listing, comment out the transition to Uploader.

You may be curious about the source of the image. While Android has the API to capture an image, in the emulator it will retrieve a manufactured image. On an actual phone it would use the phone's camera. Future versions of the emulator will likely support web cameras for more realistic image capture.

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