VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-open-camera-through-intent-and-display-captured-image-in-android/

⇱ How to Open Camera Through Intent and Display Captured Image in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Open Camera Through Intent and Display Captured Image in Android?

Last Updated : 11 Jul, 2025

The purpose of this article is to show how to open a 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 the Camera from inside our app is achieved with the help of the ACTION_IMAGE_CAPTURE Intent of MediaStore class.

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 image view.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in the Android Studio, please refer to How to Create/Start a New Project in Android Studio?

Step 2: Working with manifest file

Navigate to app > manifests > AndroidManifest.xml and add the following code under the <application> tag.

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>


Step 3: Create a xml file for setting file paths

Navigate to app > res > xml, right click on the folder and select, New > XML Resource File and set the name as file_paths.xml. Now add the following code to the file

file_paths.xml:


Step 4: Add dependency for image loading

Navigate to Gradle Scripts > build.gradle.kts (Module :app) and the following dependency under the dependencies{} scope.

dependencies {
...
implementation("com.github.bumptech.glide:glide:4.16.0")
}


Step 5: Working activity_main.xml

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

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

Syntax:

android:id="@+id/id_name"

Here the given IDs are as follows:

  • Camera Button: camera_button
  • ImageView: click_image

activity_main.xml:


Step 6: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. We will instantiate the components made in the XML file (Camera Button, ImageView) using the findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.

General Syntax:

val object: ComponentType = findViewById(R.id.IdOfTheComponent)

The Syntax for Components Used:

val openCamera: Button = findViewById(R.id.camera_open)
val clickedImage: ImageView = findViewById(R.id.click_image)

Setting up Operations on the Camera Button and ImageView.

Add the listener to the Camera button. This will be used to open the camera when the user clicks on the button.

This is done as follows: 

openCamera.setOnClickListener { }

Now, create an activity result launcher to open camera.

This is done as follows: 

if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
// load image
}

Now use the activity result launcher to get the result, here is the captured image.

This is done as follows: 

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}

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

Glide.with(this).load(photoUri).into(clickedImage)

Output:


Comment
Article Tags:

Explore