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
- Add Permissions in
AndroidManifest.xml
Add theCAMERA
andWRITE_EXTERNAL_STORAGE
permissions to your manifest file.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 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
- Button to Open Camera:
- Clicking the button triggers the
Intent
withMediaStore.ACTION_IMAGE_CAPTURE
.
- Capture Image:
- The camera opens, and the user can capture a photo.
- The
onActivityResult
callback processes the image as aBitmap
.
- 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 thebuild.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!