Take Picture with Camera Intent - Android Studio - Kotlin
📸 Take Picture with Camera Intent in Android Studio (Kotlin) — Complete Guide
Learn how to capture images using the Camera Intent in Android Studio with Kotlin. 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.
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.kt
package com.technifysoft.myapplication import android.Manifest 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.widget.ImageView import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import com.google.android.material.button.MaterialButton class MainActivityKt : AppCompatActivity() { companion object { private const val TAG = "MAIN_TAG" } private lateinit var mCaptureBtn: MaterialButton private lateinit var mImageView: ImageView private var image_uri: Uri? = null override fun onCreate(savedInstanceState: Bundle?) { 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 { //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 val cameraPermissions = arrayOf(Manifest.permission.CAMERA) requestCameraPermissions.launch(cameraPermissions) } else { //Device version is below TIRAMISU. We need Camera & Storage permissions val cameraPermissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE) requestCameraPermissions.launch(cameraPermissions) } } } private val requestCameraPermissions = registerForActivityResult( ActivityResultContracts.RequestMultiplePermissions() ) { result -> Log.d(TAG, "onActivityResult: ") Log.d(TAG, "onActivityResult: $result") //let's check if permissions are granted or not var areAllGranted = true for (isGranted in 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(this, "Camera or Storage or both permissions denied...", Toast.LENGTH_SHORT).show() } } private fun pickImageCamera() { Log.d(TAG, "pickImageCamera: ") //Setup Content values, MediaStore to capture high quality image using camera intent val contentValues = 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 = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) //Intent to launch camera val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri) cameraActivityResultLauncher.launch(intent) } private val cameraActivityResultLauncher = registerForActivityResult( ActivityResultContracts.StartActivityForResult() ) { result -> Log.d(TAG, "onActivityResult: ") //Check if image is picked or not if (result.resultCode == 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(this, "Cancelled...!", Toast.LENGTH_SHORT).show() } } }

Comments
Post a Comment