Advertisement
Android SDK

Android SDK Quick Tip: Launching the Camera

by

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:

android.provider.MediaStore.ACTION_IMAGE_CAPTURE

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(R.id.photoResultView);
image.setImageBitmap(thumbnail);
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 <uses-feature> 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:

<uses-feature android:name="android.hardware.camera"></uses-feature>

Caveat: Using the <uses-feature> 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.

Conclusion

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 androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, 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

Related Posts
  • Code
    Android SDK
    Common Entry Points for Android Applications5d7z4 preview image@2x
    In this tutorial, we will provide an overview of common entry points for Android applications. These entry points will focus on ensuring that your application maximizes its utility to the user.Read More…
  • Code
    Android SDK
    Android SDK: Next StepsAndroid preview@2x
    In this series, we've begun learning how to develop Android applications from scratch. We started exploring the development tools, got acquainted with the basic elements in an application project, looked at user interface design, interactivity, resources, and data, and we've also took a closer look at what happens when your application is running. What we've covered so far should put you in a good position to get started creating functional Android applications, but Android has a lot more to offer so the range of possibilities is virtually endless. You may therefore struggle to choose what to learn next. In this part, we'll wrap up the series by pointing out some possible directions for future learning. After this, the final part will be a quiz on what we covered throughout the series.Read More…
  • Code
    Android SDK
    Android SDK: Common Android ComponentsAndroid preview@2x
    In this series we are learning the essential features of Android development that you need to know to start building apps. So far, we've looked at the structure and typical elements in an Android app, including user interface elements and data storage. You can use what we covered already to start creating your own apps. But before you do, we will run through some common Android components in this tutorial, then have a quick look at the SDK samples in the next tutorial.Read More…
  • Code
    Android SDK
    Android SDK: Virtual & Physical DevicesAndroid preview@2x
    We are learning about Android app development in this series. We already looked at the basic elements in an Android project and touched on user interface design and data storage along the way. In this tutorial, we are going to explore how to run and interact with your apps on physical and virtual devices. In the next part of the series, we will work through the process of running an app on a physical device and on the emulator. In this part, we'll look at getting our physical and virtual devices set up in Eclipse.Read More…
  • Code
    Android SDK
    Android SDK: Create a Book Scanning App - Displaying Book InformationBookscanning series preview@2x
    With the ZXing library, you can create Android applications with barcode scanning functionality. In Android SDK: Create a Barcode Reader, we implemented basic barcode reading with the library in a simple app. In this tutorial series, we will build on what we learned to create an app that will scan books and retrieve related information about them from Google Books API.Read More…
  • Code
    Android SDK
    Android SDK: Receiving Data from the Send IntentAndroid sdk share intent sue preview image
    On the Android platform, the Intent system allows users to share content between apps. You can send and receive multiple types of data on Android but in this tutorial we will implement receiving either a text String or an image, displaying the received content in the app's user interface!Read More…