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

Pre-requisites:

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.

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

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

  • Step2: 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.

    Syntax:

    android:id="@+id/id_name"

    Here the given IDs are as follows:

  • Camera Button: camera_button
  • ImageView: click_image
  • This step will make the UI of the Application.

  • Step3: Now, after the UI, this step will create the Backend of the App. For this, open the “MainActivity.java” 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(R.id.IdOfTheComponent);



    Syntax for components used:

    Button camera_open_id= (Button)findViewById(R.id.camera_button);
    ImageView click_image_id = (ImageView)findViewById(R.id.click_image );

  • Step4: This step involves setting up the operations on the Camera Button and ImageView. These operations are as follows:
    1. 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

    2. 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() {}

    3. 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 requested pic_id. This is done as follows:

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

    4. 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) { }

    5. Then set the image reeived as result of Camera intent in the ImageView for display.


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

  • Step5: 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 MainActivity.java and activity_main.xml of Camera application is below.

activity_main.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- add Camera Button to open the Camera-->
    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginLeft="140dp"
        android:id="@+id/camera_button"
        android:text="Camera" />
  
    <!-- add ImageView to display the captured image-->
    <ImageView
        android:layout_marginTop="70dp"
        android:layout_width="350dp"
        android:layout_marginLeft="15dp"
        android:layout_height="450dp"
        android:id="@+id/click_image"
        android:layout_marginBottom="10dp"/>
</RelativeLayout>

chevron_right


MainActivity.java

filter_none

edit
close

play_arrow

link
brightness_4
code

package org.geeksforgeeks.navedmalik.camera;
  
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
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;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // By ID we can get each component
        // which id is assigned in XML file
        // get Buttons and imageview.
        camera_open_id = (Button)findViewById(R.id.camera_button);
        click_image_id = (ImageView)findViewById(R.id.click_image);
  
        // Camera_open button is for open the camera
        // and add the setOnClickListener in this button
        camera_open_id.setOnClickListener(new View.OnClickListener() {
  
            @Override
            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
                                     .ACTION_IMAGE_CAPTURE);
  
                // 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()
                               .get("data");
  
            // Set the image in imageview for display
            click_image_id.setImageBitmap(photo);
        }
    }
}

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.