Home > Uncategorized > Android SDK : Launching the Camera

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.

Step 1: Create an Android Application
Step 2: Creating the Intent
Step 3: Asking for Results

Step 4: Handling the Results
Step 5: Getting the Image
Step 6: Enforcing Device Requirements
Conclusion

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

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.

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 –

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.

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:

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;

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:

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

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.

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:

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: