How to open the camera in Android studio.

Ayush Soni
2 min readOct 27, 2022

--

Let’s Make an App that Opens the Camera Via Intent and Show in ImageView.

Steps to Make an App

Step 1 ➤

Let’s Make a Button and an ImageView in XML Button to open the camera and ImageView to show the captured Image.

<!-- add Camera Button to open the Camera --><Buttonandroid:id="@+id/camera_button"android:layout_width="100dp"android:layout_height="50dp"android:layout_marginLeft="140dp"android:text="Camera" /><!-- add ImageView to display the captured image --><ImageViewandroid:id="@+id/click_image"android:layout_width="350dp"android:layout_height="450dp"android:layout_marginLeft="15dp"android:layout_marginTop="70dp"android:layout_marginBottom="10dp" />

Step 2 ➤

Now in the MainActivity file first We will instantiate the components made in the XML file (Camera Button, ImageView) using the findViewById() method. And define the variable pic_id which is the request-id of the clicked image.

private static final int pic_id = 123

After that Create the MediaStore-provided ACTION_IMAGE_CAPTURE Intent at this point. This Intent will assist in opening the camera so that the picture may be taken. With the requested pic_id, begin the intent.

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

Use the onActivityResult() function to obtain the result.

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");
image.setImageBitmap(photo);

This is the full code for opening the camera and setting the Image in ImageView.

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 {// Define the pic id
private static final int pic_id = 123;
// Define the button and imageview type variable
Button camera;
ImageView image;
@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 = findViewById(R.id.camera_button);
image = findViewById(R.id.click_image);
// Camera button is for open the camera and add the setOnClickListener in this button
camera.setOnClickListener(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) {
super.onActivityResult(requestCode, resultCode, data);
// Match the request 'pic id with requestCode
if (requestCode == pic_id) {
// BitMap is data structure of image file which store the image in memory
Bitmap photo = (Bitmap) data.getExtras().get("data");
// Set the image in imageview for display
image.setImageBitmap(photo);
}
}
}

Many thanks for your time.

--

--