Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Taking a Picture Using an Intent

Author: Ian Darwin -- Published? true -- FormatLanguage: W

Problem:

You want to take a picture from within your app and don't want to write a lot of code.

Solution:

Create an Intent for MediaStore.ACTION_IMAGE_CAPTURE, tailor it a little, and call startActivityForResult on this Intent. Provide an onActivityResult() callback to get notified when the user is done with the camera.

Discussion:

The code example below shows the complete Camera Activity from my jpstrack application.

Assuming that you want to save the image with your application's data (instead of in the Media Gallery location), you want to provide a file-based URI referring to the target location, using intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);. Note that, according to discussion on various forum sites, the intent handler may give signicantly different results on different vendors' platforms. On the Motorola Milestone, using the Android 2.1 load from Telus Canada, with the code shown below, the defined directory gets a preview-scale image and the Media Gallery gets a 1/4-of-full-resolution (1280x960) copy. Hopefully this will be cleaned up and standardized in 2.2.

import jpstrack.android.MainActivity;
import jpstrack.android.FileNameUtils;

public class CameraNoteActivity extends Activity {

	private File imageFile;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Use an Intent to get the Camera app going.
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// Set up file to save image into.
		imageFile = new File(MainActivity.getDataDir(),
			FileNameUtils.getNextFilename("jpg"));
		Uri uri = Uri.fromFile(imageFile);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		// And away we go!
		startActivityForResult(intent, 0);
	}
	
	@Override
	public void onActivityResult(int requestCode, 
			int resultCode, Intent data) {
		switch(requestCode) {
		case 0: // take picture
			switch(resultCode) {
			case Activity.RESULT_OK:
				if (imageFile.exists())
					Toast.makeText(this, 
			"Bitmap saved as " + imageFile.getAbsoluteFile(),
				Toast.LENGTH_LONG).show();
				else {
					AlertDialog.Builder alert = 
					new AlertDialog.Builder(this);
					alert.setTitle("Error").setMessage(
					"Returned OK but image not created!").show();
				}
				break;
			case Activity.RESULT_CANCELED:
				//  no blather required!
				break;
			default:
				Toast.makeText(this, 
				"Unexpected resultCode: " + resultCode,
				Toast.LENGTH_LONG).show();
			}
			break;
		default:
				Toast.makeText(this, 
				"UNEXPECTED ACTIVITY COMPLETION",
				Toast.LENGTH_LONG).show();
		}
		finish();	// back to main app
	}
}

See Also:

Taking a Picture Using android.media.Camera - doing it this way gives you more control.

Download:

The source code for this project can be downloaded from http://www.darwinsys.com/jpstrack/.
No records found.