Image Capture Using the BuiltIn Camera Application
With mobile phones quickly becoming mobile computers, they have in many ways replaced a whole variety of consumer electronics. One of the earliest non-phone related hardware capabilities added to mobile phones was a camera. Currently, it seems someone would be hard pressed to buy a mobile phone that doesn't include a camera. Of course, Android-based phones are no exception; from the beginning, the Android SDK has supported accessing the built-in hardware camera on phones to capture images.
The easiest and most straightforward way to do many things on Android is to leverage an existing piece of software on the device by using an intent. An intent is a core component of Android that is described in the documentation as a "description of an action to be performed." In practice, intents are used to trigger other applications to do something or to switch between activities in a single application.
All stock Android devices with the appropriate hardware (camera) come with the Camera application. The Camera application includes an intent filter, which allows developers to offer image capture capabilities on a par with the Camera application without having to build their own custom capture routines.
An intent filter is a means for a programmer of an application to specify that their application offers a specific capability. Specifying an intent filter in the AndroidManifest.xml file of an application tells Android that this application and, in particular, the activity that contains the intent filter will perform the specified task, on command.
The Camera application has the following intent filter specified in its manifest file. The intent filter shown here is contained within the "Camera" activity tags.
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
In order to utilize the Camera application via an intent, we simply have to construct an intent that will be caught by the foregoing filter.
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
In practice, we probably don't want to create the intent with that action string directly. In this case, a constant is specified in the MediaStore class, ACTION_IMAGE_CAPTURE. The reason we should use the constant rather than the string itself is that if the string happens to change, it is likely that the constant will change as well, thereby making our call a bit more future-proof than it would otherwise be.
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivity(i);
Using this intent in a basic Android activity will cause the default Camera application to launch in still picture mode, as shown in Figure 1-1.

- Figure 1-1. The built-in Camera application as called from an intent shown running in an emulator
Post a comment