Wednesday, February 1, 2012

Android SDK : Launching the Camera

launch the built-in Camera application and use the results for displaying the captured image. You will achieve this by creating an Intent within your application’s Activity.
process the Intent’s results and use the resulting image within your application.

<strong>Step 1: Create an Android Application</strong>
<strong>Step 2: Creating the Intent
Step 3: Asking for Results</strong>
<strong>Step 4: Handling the Results</strong>
<strong>Step 5: Getting the Image</strong>
<strong>Step 6: Enforcing Device Requirements</strong>

1. Begin by creating an Android project. Implement your Android application as normal. Once you have a project set up and the application running, decide under what circumstances you want to launch the Camera and retrieve the resulting captured image. Will this occur when a button control is pressed? What will you do with the resulting image? Presumably, you wish to display an appropriately-sized version of the captured image as part of your Activity’s layout—for example, within an ImageView control. Implement the necessary button control, including any click handling. Design the appropriate layout with its ImageView control to contain the resulting photo. Once you have completed these tasks, you have a place to:

    Drop in the code to launch the camera
    Retrieve and display the resulting photo

2.The Camera application can be launched to take a photo with the following Intent:
android.provider.MediaStore.ACTION_IMAGE_CAPTURE </strong>
Begin by creating an Intent of this type, as follows, within your button click handler:

    <em>Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); </em>
If you just start the Activity like this, the camera application will launch. However, you might want the resulting captured image as well.

3.Intents are often started using the startActivity() method
However, in this case, your application wants to wait for, and use, the results of the Camera application’s image capture.
you want to send the Intent using the startActivityForResult() call -

<em>startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); </em>

This will launch an Activity that provides a result, and then pass that result back to the Activity that launched it.

4.When the startActivityForResult() method is called, the Activity is launched. Once that Activity finishes, the calling Activity is presented with a result within its onActivityResult() handler. Therefore, you need to implement the onActivityResult() callback method within your application’s Activity as follows:

<em>protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_PIC_REQUEST) { 
        // do something 

Yes, there it is again: CAMERA_PIC_REQUEST. This is a value that you need to define within your application as the request code returned by the Camera image capture Intent, like so:

<em>private static final int CAMERA_PIC_REQUEST = 1337; </em>

5.The image that is returned from this is appropriate for display on a small device screen. It comes in directly to the results as an Android Bitmap object:

   <em> Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); </em>

What you do with the Bitmap object is up to you. Displaying it on screen is as easy as calling the setImageBitmap() method on an ImageView you have defined in your layout (here we’ve called it photoResultView):

  <em>  ImageView image = (ImageView) findViewById(; 

<strong>A Note on Permissions:</strong>
Although your application is leveraging the Camera, it is not required to have the android.permission.CAMERA permission since it is not directly accessing the camera. Instead, it’s just launching the Camera application via Intent.

6.To offer your application only to users with devices with camera support, simply add the following tag to your application’s AndroidManifest.xml file:

<em><uses-feature android:name=""></uses-feature> </em>