We use camera access to take pictures or record videos inside an app. It lets users capture moments without leaving the app.
0
0
Camera access in Android Kotlin
Introduction
When building a photo app to take pictures.
When adding a feature to scan QR codes or barcodes.
When creating a video chat or video recording app.
When allowing users to upload images directly from the camera.
When making an app that needs to capture documents or receipts.
Syntax
Android Kotlin
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE)
This code opens the camera app to take a picture.
REQUEST_IMAGE_CAPTURE is a number you choose to identify the result later.
Examples
Starts the camera to take a photo with request code 1.
Android Kotlin
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, 1)Handles the photo taken and shows it in an ImageView.
Android Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(imageBitmap)
}
}Sample App
This app shows a button labeled "Take Photo". When you tap it, the camera opens. After taking a picture, the photo appears below the button.
Android Kotlin
import android.app.Activity import android.content.Intent import android.graphics.Bitmap import android.os.Bundle import android.provider.MediaStore import android.widget.Button import android.widget.ImageView class MainActivity : Activity() { private val REQUEST_IMAGE_CAPTURE = 1 private lateinit var imageView: ImageView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) imageView = ImageView(this) val button = Button(this).apply { text = "Take Photo" } button.setOnClickListener { val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE) } val layout = android.widget.LinearLayout(this).apply { orientation = android.widget.LinearLayout.VERTICAL addView(button) addView(imageView) } setContentView(layout) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { val imageBitmap = data?.extras?.get("data") as Bitmap imageView.setImageBitmap(imageBitmap) } } }
OutputSuccess
Important Notes
Remember to add android.permission.CAMERA in your AndroidManifest.xml.
From Android 6.0+, you must ask for camera permission at runtime.
Using startActivityForResult is now deprecated; consider using registerForActivityResult in newer apps.
Summary
Camera access lets your app take photos or videos.
Use an Intent with MediaStore.ACTION_IMAGE_CAPTURE to open the camera.
Handle the result to get the photo and show it in your app.