Tue. Jan 14th, 2025

To open the camera in Android, you can use an Intent to launch the camera app. After capturing a photo, the app can retrieve the captured image. Below is a detailed example.


Steps to Open Camera and Capture an Image

  1. Add Permissions in AndroidManifest.xml
    Add the CAMERA and WRITE_EXTERNAL_STORAGE permissions to your manifest file.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. Create a Function to Open the Camera

Example Code

MainActivity.kt

package com.example.opencameraexample

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            CameraScreen()
        }
    }

    @Composable
    fun CameraScreen() {
        var capturedImage by remember { mutableStateOf<Bitmap?>(null) }

        // Launch the camera and capture an image
        val openCameraLauncher = rememberLauncherForActivityResult(
            contract = ActivityResultContracts.StartActivityForResult()
        ) { result ->
            if (result.resultCode == Activity.RESULT_OK) {
                val imageBitmap = result.data?.extras?.get("data") as Bitmap
                capturedImage = imageBitmap
            }
        }

        Scaffold(
            topBar = { TopAppBar(title = { Text("Open Camera Example") }) }
        ) { padding ->
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
                    .padding(16.dp),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Button(onClick = {
                    val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                    openCameraLauncher.launch(cameraIntent)
                }) {
                    Text("Open Camera")
                }

                Spacer(modifier = Modifier.height(20.dp))

                capturedImage?.let {
                    Image(
                        bitmap = it.asImageBitmap(),
                        contentDescription = "Captured Image",
                        modifier = Modifier.size(300.dp)
                    )
                }
            }
        }
    }
}

How It Works

  1. Button to Open Camera:
  • Clicking the button triggers the Intent with MediaStore.ACTION_IMAGE_CAPTURE.
  1. Capture Image:
  • The camera opens, and the user can capture a photo.
  • The onActivityResult callback processes the image as a Bitmap.
  1. Display Image:
  • The captured image is displayed in an Image composable using Jetpack Compose.

Dependencies

  • Jetpack Compose:
    Ensure your project includes the required dependencies for Compose in the build.gradle file.

Notes:

  • The captured image is returned as a low-resolution thumbnail using data.extras.get("data"). For full-resolution images, you’ll need to save the image to a file and retrieve it using a URI.
  • Let me know if you’d like an example for saving the image to a file and handling higher resolutions!

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *