Android Code to Capture Image Using the Built-In Camera App

The simplest way to capture an image is to use Android’s built-In camera app capabilities via the intent filter. The intent filter of the built in camera app is named:

android.media.action.IMAGE_CAPTURE

Accordingly to access this function you simply need to create an intent that would be caught by the above filter. In addition you need to procesws the image returned as a result of camera click.

Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(i, CAMERA_RESULT);

 

 

Here’s a basic example that starts a new activity with intent

package com.knowpapa.camerademo; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.graphics.Bitmap; 
import android.widget.ImageView; 

public class CameraDemo extends Activity { 

final static int CAMERA_OUTPUT = 0; 

ImageView imv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.camerapreview); 
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(i, CAMERA_RESULT); 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
super.onActivityResult(requestCode, resultCode, intent); 

if (resultCode == RESULT_OK) 
{ 
Get Bundle extras = intent.getExtras(); 
Bitmap bmp = (Bitmap) extras.get("data"); 
imv = (ImageView) findViewById(R.id.ResultingImage); 
imv.setImageBitmap(bmp); 
} 
} 
}

Notice that the image returned by the camera application is passed as an extra named ‘data’ through to the intent of the calling activity. The ‘data’ contains a Bitma object,that we cast  to a generic Object.

Bundle extras = intent.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");

A sample associated layout file in res/layout/camerapreview.xml is a s follows:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
>
<ImageView android:id="@+id/ResultingImage" android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView> 
</LinearLayout>

And finally the contents of AndroidManifest.xml for the sake of completeness:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
android:versionCode="1" 
android:versionName="1.0" package="com.knowpapa.camerademo"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".CameraDemo" 
android:label="@string/app_name"> 
<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 
</application> 
<uses-sdk android:minSdkVersion="4" /> 
</manifest>

An alternative method to capture images uses the actual camera hardware module to access camera hardware directly thus providing more control over what you can do with the camera. This is demonstrated in the next example.

android.hardware.camera