ACTION_IMAGE_CAPTURE intent for taking image in Android

Permissions required in the manifest file.

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

The layout activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_my_picture"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:scaleType="centerCrop"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="Take Picture"
            android:onClick="takePicture"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="Delete Picture"
            android:onClick="deletePicture"/>
    </LinearLayout>

</LinearLayout>

The MainActivity.java, the take picture button in this activity will fire an ACTION_IMAGE_CAPTURE intent to open the camera, after a picture is taken and saved, it will return to this activity calling the onActivityResult method will be called.

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private static final int    REQUEST_TAKE_IMAGE                = 1;
    private static String imagePath = "";
    private ImageView ivMyPicture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivMyPicture = (ImageView) findViewById(R.id.iv_my_picture);

        ivMyPicture.post(new Runnable() {
            @Override
            public void run() {
                loadImage();
            }
        });
    }

    private void loadImage() {
        if (imagePath.isEmpty()) return;

        Bitmap myPictureBitmap = BitmapFactory.decodeFile(imagePath);
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
            myPictureBitmap = Bitmap.createScaledBitmap(myPictureBitmap, ivMyPicture.getWidth(),ivMyPicture.getHeight(),true);
        }
        ivMyPicture.setImageBitmap(myPictureBitmap);
    }

    public void takePicture(View v) {
        File   storageDir    = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File   image         = new File(storageDir.getAbsolutePath() + "/my_picture.jpg");
        imagePath = image.getAbsolutePath();
        Log.d("takePicture", "picture will be saved at: " + imagePath);

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
        startActivityForResult(takePictureIntent, REQUEST_TAKE_IMAGE);
    }

    public void deletePicture(View v) {
        ivMyPicture.setImageBitmap(null);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TAKE_IMAGE && resultCode == Activity.RESULT_OK) {
            loadImage();
            //            Bundle extras = data.getExtras();
            //            Bitmap myPictureBitmap = (Bitmap) extras.get("data");
        }
    }

}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search