How to Open Camera Through Intent and Display Captured Image in Android?
Last Updated :
09 Feb, 2023
Pre-requisites:
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 Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android. This will create an XML file “activity_main.xml” and a Java/Kotlin File “MainActivity”. Please refer to the prerequisites to learn more about this step.
Step 2: Working with the XML Files
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
This step will make the UI of the Application.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< Button
android:id = "@+id/camera_button"
android:layout_width = "100dp"
android:layout_height = "50dp"
android:layout_marginStart = "150dp"
android:text = "Camera" />
< ImageView
android:id = "@+id/click_image"
android:layout_width = "350dp"
android:layout_height = "450dp"
android:layout_marginStart = "30dp"
android:layout_marginTop = "70dp"
android:layout_marginBottom = "10dp" />
</ RelativeLayout >
|
Step 3: 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:
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
The Syntax for Components Used:
Button camera_open_id= findViewById(R.id.camera_button);
ImageView click_image_id = findViewById(R.id.click_image);
Setting up Operations on the Camera Button and ImageView.
First, define the variable pic_id which is the request-id of the clicked image.
This is done as follows:
private static final int pic_id = 123
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:
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 the onActivityResult() method to get the result, here is the captured image.
This is done as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { }
Then set the image received as a result of Camera intent in the ImageView for display.
Bitmap photo = (Bitmap) data.getExtras().get("data");
clicked_image_id.setImageBitmap(photo);
Java
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int pic_id = 123 ;
Button camera_open_id;
ImageView click_image_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera_open_id = findViewById(R.id.camera_button);
click_image_id = findViewById(R.id.click_image);
camera_open_id.setOnClickListener(v -> {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera_intent, pic_id);
});
}
protected void onActivityResult( int requestCode, int resultCode, Intent data) {
super .onActivityResult(requestCode, resultCode, data);
if (requestCode == pic_id) {
Bitmap photo = (Bitmap) data.getExtras().get( "data" );
click_image_id.setImageBitmap(photo);
}
}
}
|
Kotlin
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var cameraOpenId: Button
lateinit var clickImageId: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraOpenId = findViewById(R.id.camera_button)
clickImageId = findViewById(R.id.click_image)
cameraOpenId.setOnClickListener(View.OnClickListener { v: View? ->
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, pic_id)
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super .onActivityResult(requestCode, resultCode, data)
if (requestCode == pic_id) {
val photo = data!!.extras!![ "data" ] as Bitmap?
clickImageId.setImageBitmap(photo)
}
}
companion object {
private const val pic_id = 123
}
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...