Take Picture with Camera Intent - Android Studio - Java

📸 Take Picture with Camera Intent in Android Studio (Java) — Complete Guide

Learn how to capture images using the Camera Intent in Android Studio with Java. This step-by-step tutorial covers everything you need to implement a built-in camera feature in your Android app. You’ll understand how to open the camera, handle permissions, save the captured image, and display it inside your application.

Whether you’re a beginner or an experienced Android developer, this guide will help you integrate camera functionality quickly and efficiently using clean and simple Java code.

What this post covers:

  • Requesting camera permissions

  • Launching the Camera Intent

  • Receiving the captured image

  • Displaying and saving the photo

  • Common errors & their solutions

Perfect for Android developers building apps that require image capturing features such as profile photos, scanning, media apps, and more.


VIDEO


Code:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <!--Adding Camera, Write External Storage Permission-->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">

    <!--Image view in which image will be set-->
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_image_black" />

    <!--Button to capture image-->
    <com.google.android.material.button.MaterialButton
        android:id="@+id/capture_image_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Capture Image" />

</LinearLayout>

MainActivity.java

package com.technifysoft.myapplication;

import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.button.MaterialButton;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MAIN_TAG";

    private MaterialButton mCaptureBtn;
    private ImageView mImageView;

    private Uri image_uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.image_view);
        mCaptureBtn = findViewById(R.id.capture_image_btn);

        //button click
        mCaptureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Camera is clicked we need to check if we have permission of Camera, Storage before launching Camera to Capture image
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    //Device version is TIRAMISU or above. We only need Camera permission
                    String[] cameraPermissions = new String[]{android.Manifest.permission.CAMERA};
                    requestCameraPermissions.launch(cameraPermissions);
                } else {
                    //Device version is below TIRAMISU. We need Camera & Storage permissions
                    String[] cameraPermissions = new String[]{android.Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    requestCameraPermissions.launch(cameraPermissions);
                }
            }
        });
    }

    private ActivityResultLauncher<String[]> requestCameraPermissions = registerForActivityResult(
            new ActivityResultContracts.RequestMultiplePermissions(),
            new ActivityResultCallback<Map<String, Boolean>>() {
                @Override
                public void onActivityResult(Map<String, Boolean> result) {
                    Log.d(TAG, "onActivityResult: ");
                    Log.d(TAG, "onActivityResult: " + result.toString());

                    //let's check if permissions are granted or not
                    boolean areAllGranted = true;
                    for (Boolean isGranted : result.values()) {

                        areAllGranted = areAllGranted && isGranted;
                    }

                    if (areAllGranted) {
                        //All Permissions Camera, Storage are granted, we can now launch camera to capture image
                        pickImageCamera();
                    } else {
                        //Camera or Storage or Both permissions are denied, Can't launch camera to capture image
                        Toast.makeText(MainActivity.this, "Camera or Storage or both permissions denied...", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );

    private void pickImageCamera() {
        Log.d(TAG, "pickImageCamera: ");
        //Setup Content values, MediaStore to capture high quality image using camera intent
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.Media.TITLE, "TEMPORARY_IMAGE");
        contentValues.put(MediaStore.Images.Media.DESCRIPTION, "TEMPORARY_IMAGE_DESCRIPTION");
        //Uri of the image to be captured from camera
        image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        //Intent to launch camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
        cameraActivityResultLauncher.launch(intent);
    }

    private final ActivityResultLauncher<Intent> cameraActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    Log.d(TAG, "onActivityResult: ");
                    //Check if image is picked or not
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        //no need to get image uri here we will have it in pickImageCamera() function

                        Log.d(TAG, "onActivityResult: imageUri: " + image_uri);

                        mImageView.setImageURI(image_uri);
                    } else {
                        //Cancelled
                        Toast.makeText(MainActivity.this, "Cancelled...!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
    );

}

Screenshots:

Take Picture with Camera - Android Studio - Java

Comments

Popular posts from this blog

Picture In Picture - Android Studio - Kotlin

Manage External Storage Permission - Android Studio - Kotlin

How to add AIDL folder | Android Studio