We use camera access to take pictures or record videos inside an app. It lets users capture moments without leaving the app.
Camera access in 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.
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, 1)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)
}
}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.
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) } } }
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.
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.
Practice
MediaStore.ACTION_IMAGE_CAPTURE in an Android app?Solution
Step 1: Understand the intent action
MediaStore.ACTION_IMAGE_CAPTUREis a predefined intent action to launch the camera app for capturing images.Step 2: Identify the purpose in the app
This intent opens the camera so the user can take a photo, which the app can then receive.Final Answer:
To open the device camera to take a photo -> Option AQuick Check:
Camera intent = open camera [OK]
- Confusing camera intent with gallery intent
- Thinking it records audio
- Assuming it accesses location
Solution
Step 1: Check the method signature
startActivityForResultrequires an Intent and a request code integer.Step 2: Verify the Intent creation
The Intent must be created withMediaStore.ACTION_IMAGE_CAPTUREas the action.Final Answer:
startActivityForResult(Intent(MediaStore.ACTION_IMAGE_CAPTURE), REQUEST_CODE) -> Option AQuick Check:
Intent + request code = startActivityForResult [OK]
- Passing action string instead of Intent
- Omitting request code
- Swapping Intent and request code parameters
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
val photo = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(photo)
}
}Solution
Step 1: Understand onActivityResult behavior
This method receives the result from the camera intent when the user takes a photo.Step 2: Check the code logic
If the request and result codes match, it extracts the photo bitmap from the intent extras and sets it to the imageView.Final Answer:
The photo taken by the user will be displayed in imageView -> Option DQuick Check:
Photo bitmap set to imageView = display photo [OK]
- Not checking requestCode or resultCode
- Casting data extras incorrectly
- Forgetting to call super.onActivityResult
val intent = Intent() intent.action = MediaStore.ACTION_IMAGE_CAPTURE startActivityForResult(intent)
Solution
Step 1: Check startActivityForResult usage
This method requires two parameters: an Intent and a request code integer.Step 2: Identify missing parameter
The code calls startActivityForResult with only the Intent, missing the request code.Final Answer:
Missing request code parameter in startActivityForResult -> Option BQuick Check:
startActivityForResult needs Intent + request code [OK]
- Omitting request code parameter
- Using wrong Intent constructor
- Misunderstanding startActivityForResult signature
Solution
Step 1: Understand full-size photo saving
To save a full-size photo, you must provide a file URI where the camera app saves the image.Step 2: Use Intent extra MediaStore.EXTRA_OUTPUT
Pass the file URI as an extra in the camera intent usingMediaStore.EXTRA_OUTPUTbefore starting the intent.Step 3: Start camera intent with this setup
This ensures the camera saves the full photo to your file, not just a thumbnail in data extras.Final Answer:
Create a file URI, pass it with Intent extra MediaStore.EXTRA_OUTPUT, then start camera intent -> Option CQuick Check:
File URI + EXTRA_OUTPUT = full photo saved [OK]
- Relying on thumbnail from data extras only
- Not providing file URI to camera intent
- Using video capture intent for photos
Ready to prove it?
Pass the test and this pattern moves unknown
