Using Intents to Take Pictures

The easiest way to take a picture using the device camera is using the action_image_capture Media Store static constant in an Intent passed to startActivityForResult.

startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),

TAKE_PICTURE);

This will launch the camera Activity, allowing users to modify the image settings manually, and preventing you from having to rewrite the entire camera application.

The image capture action supports two modes, thumbnail and full image.

> Thumbnail By default, the picture taken by the image capture action will return a thumbnail Bitmap in the data extra within the Intent parameter returned in onActivityResult. As shown in Listing 11-11, call getParcelableExtra specifying the extra name data on the Intent parameter to return the thumbnail as a Bitmap.

> Full image If you specify an output URI using a MediaStore.EXTRA_0UTPUT extra in the launch Intent, the full-size image taken by the camera will be saved to the specified location. In this case no thumbnail will be returned in the Activity result callback and the result Intent data will be null.

Listing 11-11 shows how to use the image capture action to capture either a thumbnail or full image using an Intent.

LISTING 11-11: Taking a picture using an Intent

Available for download on private static int TAKE_PICTURE = 1; Wrox.com private Uri outputFileUri;

private void getThumbailPicture() {

Intent intent = new Intent(MediaStore.ACTI0N_IMAGE_CAPTURE); startActivityForResult(intent, TAKE_PICTURE);

private void saveFullImage() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = new File(Environment.getExternalStorageDirectory(),

"test.jpg"); outputFileUri = Uri.fromFile(file);

intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE);

©Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE) { Uri imageUri = null;

// Check if the result includes a thumbnail Bitmap if (data != null) {

Bitmap thumbnail = data.getParcelableExtra("data"); // TODO Do something with the thumbnail

// TODO Do something with the full image stored // in outputFileUri

Once you have taken the picture, you can either add it to the Media Store as shown later in this chapter, or process it for use within your application before removing it.

Photoshop Secrets

Photoshop Secrets

Are You Frustrated Because Your Graphics Are Not Looking Professional? Have You Been Slaving Over Your Projects, But Find Yourself Not Getting What You Want From Your Generic Graphic Software? Well, youre about to learn some of the secrets and tips to enhance your images, photos and other projects that you are trying to create and make look professional.

Get My Free Ebook


Post a comment