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


Android Mobility: Open Source Hits the Road : Page 2

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

Take a Picture
One of Android's more striking features is that it allows you to define your user interface in XML. This provides a nice middle ground between the extremes of specifying all your layout in code, which can be hard to maintain, or using an external visual editing tool, which tends to be inflexible and generate inscrutable code.

The camera screen should both instruct the user to shoot and also provide a way to navigate through the app. This XML, placed in /res/layout/camera.xml or a similar file, will generate an appropriate screen:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="@string/tapToShoot" />
Any value that starts with @string is defined in /res/values/strings.xml (Listing 1). This allows for easier localization.

At this point, you can run the application and see your layout—without having added one line of Java code. As you may expect, you can see two Buttons: a large one that fills most of the screen and a smaller one at the bottom. The sizing effect is accomplished through the layout_weight flag, which instructs Android to provide this Button with any leftover available space. Because you do not know how large the button will be, set the initial height to 0dip (that represents Device Independent Pixels) so Android can use an optimized algorithm to calculate the appropriate sizes. You can experiment using different size emulators (HVGA vs. QVGA, for example) and different orientations (Portrait vs. Horizontal), but the layout will always be resized to fit the screen and look good.

Going back to the Camera Activity, it's now time to actually take a picture. Retrieve the cameraInstructions item, and attach a hook to perform the action.

Button click = (Button)findViewById(R.id.cameraInstructions);
click.setOnClickListener(new View.OnClickListener () {
    public void onClick(View v)
        // Action goes here.
Android provides a CameraDevice as part of its android.hardware package. Listing 2 shows that the code for capturing an image is fairly simple.

Figure 1. DDMS File Explorer in Eclipse: Navigating the phone file system to locate a file.

Several components interact here:

  1. The CameraDevice allows you to configure the camera and take a picture.
  2. The Bitmap stores actual image pixel data.
  3. The Canvas connects your camera to the storage. Note that you could use a screen Canvas to preview instead of saving.
  4. The FileOutputStream writes the data to disk.
Go ahead and run the application. By setting a debugger in the above code, you can step through and see the actions it takes.

In Eclipse, open the DDMS perspective and locate your file in the File Explorer under /data/data/your.package.name (shown in Figure 1).

You can copy the picture.jpg image to your hard drive for a clearer view.

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