Camera photo capture in android – Source Code

Most Android devices have at least one camera application by default. To capture photos, you can delegate the action of taking a photo to one of the existing camera applications instead of creating your own Camera activity. In this article, we will see how to take a picture with camera using existing camera application.

Step – 1 : activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    tools:context=".MainActivity"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/tvHasCamera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"     
        android:background="#FF0000"
        android:textColor="#FFF"
        android:textSize="18dp"
        android:layout_marginBottom="5dp"
        android:text="Has Camera? " />
 
    <TextView
        android:id="@+id/tvHasCameraApp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="#FF0000"
        android:textColor="#FFF"   
        android:textSize="18dp"
        android:text="Has Camera App?" />
 
    <ImageView
        android:id="@+id/ivThumbnailPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"     
        android:src="@drawable/ic_launcher" />
 
    <Button
        android:id="@+id/btnTakePic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"     
        android:text="Take a Picture" />
 
</LinearLayout>

Step 2 : MainActivity.java

package com.hmkcode.android;
 
import java.io.File;
import java.util.List;
 
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MainActivity extends Activity implements OnClickListener {
 
    Button btnTackPic;
    TextView tvHasCamera, tvHasCameraApp;
    ImageView ivThumbnailPhoto;
    Bitmap bitMap;
    static int TAKE_PICTURE = 1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Get reference to views
        tvHasCamera = (TextView) findViewById(R.id.tvHasCamera);
        tvHasCameraApp = (TextView) findViewById(R.id.tvHasCameraApp);
        btnTackPic = (Button) findViewById(R.id.btnTakePic);
        ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
 
        // Does your device have a camera?
        if(hasCamera()){
            tvHasCamera.setBackgroundColor(0xFF00CC00);
            tvHasCamera.setText("You have Camera");
        }
 
        // Do you have Camera Apps?
        if(hasDefualtCameraApp(MediaStore.ACTION_IMAGE_CAPTURE)){
            tvHasCameraApp.setBackgroundColor(0xFF00CC00);
            tvHasCameraApp.setText("You have Camera Apps");
        }
 
        // add onclick listener to the button
        btnTackPic.setOnClickListener(this);
 
    }
 
    // on button "btnTackPic" is clicked
    @Override
    public void onClick(View view) {
 
        // create intent with ACTION_IMAGE_CAPTURE action 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 
        // start camera activity
        startActivityForResult(intent, TAKE_PICTURE);
 
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
 
        if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null){
            // get bundle
            Bundle extras = intent.getExtras();
 
            // get bitmap
            bitMap = (Bitmap) extras.get("data");
            ivThumbnailPhoto.setImageBitmap(bitMap);
 
        }
    }
 
    // method to check if you have a Camera
    private boolean hasCamera(){
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }
 
    // method to check you have Camera Apps
    private boolean hasDefualtCameraApp(String action){
        final PackageManager packageManager = getPackageManager();
        final Intent intent = new Intent(action);
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
 
        return list.size() > 0;
 
    }
}

Now you can take a picture and display it on your activity:

Step 3 : Saving the Photo

Do not forget to add permission to the AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Update the onClick() method as following:

public void onClick(View view) {
 
        // create intent with ACTION_IMAGE_CAPTURE action 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 
                // this part to save captured image on provided path
        File file = new File(Environment.getExternalStorageDirectory(),
        "my-photo.jpg");
        Uri photoPath = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
 
        // start camera activity
        startActivityForResult(intent, TAKE_PICTURE);
 
    }

The the captured photo will be saved under Pictures/, the standard location for shared photos on the external/shared storage.

Step 4 : Request Camera Permission

<uses-feature android:name="android.hardware.camera" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hmkcode.android"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.hmkcode.android.MainActivity"
            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>
 
</manifest>

Enjoy….