Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Android | How to open Camera through Intent and display captured image

  • Last Updated : 20 Nov, 2021


The purpose of this article is to show how to open Camera from inside an App and click the image and then display this image inside the same app. An android application has been developed in this article to achieve this. The opening of Camera from inside our app is achieved with the help of ACTION_IMAGE_CAPTURE Intent of MediaStore class.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

For Example:

This image shows the Image clicked by the camera and set in Imageview. When the app is opened, it displays the “Camera” Button to open the camera. When pressed, ACTION_IMAGE_CAPTURE Intent gets started by the MediaStore class. When the image is captured, it is displayed in the imageview.

How to create an App to open Camera through Intent and display captured image

Step 1: Firstly create a new Android Application. This will create an XML file “activity_main.xml” and a Java File “MainActivity.Java”. Please refer to the pre-requisites to learn more about this step.

Step 2: Open “activity_main.xml” file and add following widgets in a Relative Layout: 

  • A Button to open the Camera
  • An ImageView to display the captured image

Also, Assign the ID to each component along with other attributes as shown in the image and the code below. The assigned ID on a component helps that component to be easily found and used in the Java files.



Here the given IDs are as follows:

  • Camera Button: camera_button
  • ImageView: click_image

This step will make the UI of the Application.

Step 3: Now, after the UI, this step will create the Backend of the App. For this, open the “” file and instantiate the components made in the XML file (Camera Button, ImageView) using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.

General Syntax: 

ComponentType object = (ComponentType)findViewById(;

Syntax for components used: 

Button camera_open_id= (Button)findViewById(; 
ImageView click_image_id = (ImageView)findViewById( ); 

Step 4: This step involves setting up the operations on the Camera Button and ImageView. These operations are as follows: 

  • First define the variable pic_id that is request id of clicked image. This is done as follows: 

private static final int pic_id = 123

  •  Add the listener on the Camera button. This will be used to open the camera when the user clicks on the button. This is done as follows: 

camera_open_id.setOnClickListener(new View.OnClickListener() {}

  •  Now create the ACTION_IMAGE_CAPTURE Intent provided by MediaStore. This Intent will help to open the camera for capturing the image. Start the intent with the requested pic_id. This is done as follows: 

Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(camera_intent, pic_id); 

  • Now use onActivityResult() method to get the result, here the captured image. This is done as follows: 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { } 

  • Then set the image received as result of Camera intent in the ImageView for display. 

Bitmap photo = (Bitmap) data.getExtras().get(“data”); 

Step 5: Now Run the app and operate as follows: 

  • When the app is opened, it displays a “Camera” Button. Click on it to open the Camera.
  • Capture the image and display the captured image.

Complete code of and activity_main.xml of Camera application is below.

Filename: activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
    <!-- add Camera Button to open the Camera-->
        android:text="Camera" />
    <!-- add ImageView to display the captured image-->



import android.content.Intent;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    // Define the pic id
    private static final int pic_id = 123;
    // Define the button and imageview type variable
    Button camera_open_id;
    ImageView click_image_id;
    protected void onCreate(Bundle savedInstanceState)
        // By ID we can get each component
        // which id is assigned in XML file
        // get Buttons and imageview.
        camera_open_id = (Button)findViewById(;
        click_image_id = (ImageView)findViewById(;
        // Camera_open button is for open the camera
        // and add the setOnClickListener in this button
        camera_open_id.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
                // Create the camera_intent ACTION_IMAGE_CAPTURE
                // it will open the camera for capture the image
                Intent camera_intent
                    = new Intent(MediaStore
                // Start the activity with camera_intent,
                // and request pic id
                startActivityForResult(camera_intent, pic_id);
    // This method will help to retrieve the image
    protected void onActivityResult(int requestCode,
                                    int resultCode,
                                    Intent data)
        // Match the request 'pic id with requestCode
        if (requestCode == pic_id) {
            // BitMap is data structure of image file
            // which stor the image in memory
            Bitmap photo = (Bitmap)data.getExtras()
            // Set the image in imageview for display



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!