Android Capture Image From Camera And Gallery

In this tutorial i will demonstrate you how to capture an image from camera and pick an image from gallery and then display it in an imageView by creating a simple ImagePickerApp.

 

 

Create new project

1. Create a new project in Android Studio from File ⇒ New Project by filling the required details. When it prompts you to select the activity, choose Empty Activity and continue.

2. Open AndroidManifest.xml and add the following permissions :

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

To advertise that your application depends on having a camera, put a <uses-feature> tag of name=”android.hardware.camera” in your manifest file.By adding android.hardware.camera, Play Store detects and prevents installing the application on devices with no camera.

By setting android:required to false ,Google Play will allow devices without a camera to download your application.

3. Open activity_main.xml and write the below code.It contains one imageView and two buttons Camera and Gallery ,btn_camera is to capture image from camera and btn_gallery is to pick image from gallery.

activity_main.xml

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

    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/background"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_camera"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:text="camera"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:text="gallery"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:textStyle="bold" />


    </LinearLayout>

</LinearLayout>

4. Open MainActivity.java and write the below code:

MainActivity.java

package com.example.cameratest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Button btn_camera, btn_gallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image);
        btn_camera = (Button) findViewById(R.id.btn_camera);
        btn_gallery = (Button) findViewById(R.id.btn_gallery);


        btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //To take picture from camera

                Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(takePicture, 0);//zero can be replaced with any action code

            }
        });
        btn_gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //To pick photo from gallery

                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
            }
        });

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        switch (requestCode) {
            case 0:
                if (resultCode == RESULT_OK) {
                    Bundle extras = imageReturnedIntent.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
                    imageView.setImageBitmap(imageBitmap);
                }

                break;
            case 1:
                if (resultCode == RESULT_OK) {
                    Uri selectedImage = imageReturnedIntent.getData();
                    imageView.setImageURI(selectedImage);
                }
                break;
        }
    }
}

Intent is the standard way to delegate actions to another application.

  • To start the native camera the Intent requires android.provider.MediaStore.ACTION_IMAGE_CAPTURE.
  • To pick an image from gallery, the Intent requires the following argument : Intent.ACTION_PICK.

Now we know how to capture and pick image from camera and gallery now its time to set image in an imageView.

startActivityForResult() contains intent and related request code returns an intent to onActivityResult() .Inside onActivityResult() we get the Uri from the Intent and set it in imageView as shown below:

Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);

 

When you run your app it will look like this as shown below:

      

I hope this post is helpful for you in understanding how to Capture Image From Camera and Select Image From Gallery in an android application.

4 thoughts on “Android Capture Image From Camera And Gallery”

  1. Hi,

    The code is working for Gallery but not for camera.

    The camera initialises and takes the picture but it does not show the picture in ImageView.

    Could you please advise.

    Thank you.

    1. Arun Chandravanshi

      Hi S K Jain
      Thank you for bringing it 🙂 Yes there is the issue with camera-related code in the onActivityResult() method 🙁
      I have updated the post with the fix.
      For your issue you just update your onActivityResult() code for the camera with the below code :

      Bundle extras = imageReturnedIntent.getExtras();
      Bitmap imageBitmap = (Bitmap) extras.get(“data”);
      imageView.setImageBitmap(imageBitmap);

    1. Arun Chandravanshi

      Hey Manish,
      to select multiple images need to create own file system, I will do it in my future post soon.

Leave a Reply