Android sdk quick tip: launching the camera

GFX9.COM share Android sdk quick tip: launching the camera, you can download now.

This quick tip shows you how to 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. You’ll then learn how to get and process the Intent’s results and use the resulting image within your application.

Step 1: Create an Android Application

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:

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

Now you are ready to proceed with this quick tip.

Step 2: Creating the Intent

The Camera application can be launched to take a photo with the following Intent:


Begin by creating an Intent of this type, as follows, within your button click handler:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

If you just start the Activity like this, the camera application will launch. However, you might want the resulting captured image as well. Luckily, you can retrieve a View-friendly version of the captured camera image as part of the Intent results data.

Step 3: Asking for Results

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. Therefore, you want to send the Intent using the startActivityForResult() call instead. This way you can inspect the results and use the captured image. Therefore, the code should look something like this:

startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

This will launch an Activity that provides a result, and then pass that result back to the Activity that launched it. If you’re now wondering where CAMERA_PIC_REQUEST comes from, read on.

Step 4: Handling the Results

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:

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:

private static final int CAMERA_PIC_REQUEST = 1337;

Beyond that, you can use this value to differentiate between different types of results. In this case, just verify that the requestCode is the one you set.

Step 5: Getting the Image

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:

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

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):

ImageView image = (ImageView) findViewById(;
Android SDK Take Photo

A Note on Permissions:
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.

Step 6: Enforcing Device Requirements

So, you’ve seen that it only takes a couple of minutes to add camera capture features to your application. But, guess what? Not all of your users will have Android devices with built-in cameras. Uh-oh!

Luckily, there is a way to help avoid this problem: the tag of the Android Manifest file. This tells Android Market (and any other interested parties) that your application makes use of the camera and to filter your application accordingly.

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

Caveat: Using the does not guarantee that your app will not be installed on a device without a camera, even if it’s absolutely required. If you only publish through Android Market, though, users with devices without a camera will not see your app listed.


In this quick tip you have learned how to launch the built-in camera application using an Intent and how to retrieve a version of the captured image for use within your own application. Although the resulting image is not the full sized image, the image is much more appropriate for mobile screens and is the fastest and most efficient way to retrieve and display the captured image from the camera. You can, in fact, retrieve the full sized image (often several megapixels and not ideal for display)—but that, my friends, is for another tutorial. ☺

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to [email protected] /* */ , via their blog at, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

submit to reddit

Similar content