Returning Data from the Camera


Build your own Android App Dev Empire

Get Instant Access

Of course, simply capturing an image using the built-in camera application won't actually be useful without having the Camera application return the picture to the calling activity when one is captured. This can be accomplished by substituting the startActivity method in our activity with the startActivityForResult method. Using this method allows us the ability to access the data returned from the Camera application, which happens to be the image that was captured by the user as a Bitmap.

Here is a basic example:

package com.apress.proandroidmedia.ch1.cameraintent;

import; import android.content.Intent; import; import android.os.Bundle; import android.widget.ImageView;

public class CameraIntent extends Activity {

final static int CAMERA_RESULT = 0;

ImageView imv;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(i, CAMERA_RESULT);

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent);

Get Bundle extras = intent.getExtras(); Bitmap bmp = (Bitmap) extras.get("data");

imv = (ImageView) findViewById(; imv.setImageBitmap(bmp);

It requires the following in the project's layout/main.xml file: <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<ImageView android:id="@+id/ReturnedImageView" android:layout_width="wrap_content"^ android:layout_height="wrap_content"></ImageView>


To complete the foregoing example, here are the contents of AndroidManifest.xml. <?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="" android:versionCode="1"

android:versionName="1.0" package="com.apress.proandroidmedia.ch1.cameraintent"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CameraIntent"

android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

<uses-sdk android:minSdkVersion="4" /> </manifest>

In this example, the image is returned from the Camera application in an extra passed through the intent that is sent to our calling activity in the onActivityResult method. The name of the extra is "data" and it contains a Bitmap object, which needs to be cast from a generic Object.

// Get Extras from the intent Bundle extras = intent.getExtras();

// Get the returned image from that extra Bitmap bmp = (Bitmap) extras.get("data");

In our layout XML (layout/main.xml) file, we have an ImageView. An ImageView is an extension of a generic View, which supports the display of images. Since we have an ImageView with the id ReturnedImageView specified, in our activity we need to obtain a reference to that and set its Bitmap through its setImageBitmap method to be our returned image. This enables the user of our application to view the image that was captured.

To get a reference to the ImageView object, we use the standard findViewById method specified in the Activity class. This method allows us to programmatically reference elements specified in the layout XML file that we are using via setContentView by passing in the id of the element. In the foregoing example, the ImageView object is specified in the XML as follows:

<ImageView android:id="@+id/ReturnedImageView" android:layout_width="wrap_content"^ android:layout_height="wrap_content"></ImageView>

To reference the ImageView and tell it to display the Bitmap from the Camera, we use the following code.

imv = (ImageView) findViewById(;imv.setImageBitmap(bmp);

When you run this example, you'll probably notice that the resulting image is small. (On my phone, it is 121 pixels wide by 162 pixels tall. Other devices have different default sizes.) This is not a bug — rather, it is by design. The Camera application, when triggered via an intent, does not return the full-size image back to the calling activity. In general, doing so would require quite a bit of memory, and the mobile device is generally constrained in this respect. Instead the Camera application returns a small thumbnail image in the returned intent, as shown in Figure 1-2.

Figure 1-2. The resulting 121x162 pixel image displayed in our ImageVlew

Was this article helpful?

0 0

Post a comment