Challenge - 5 Problems
Camera Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Camera Preview Display
What will be the visible result when this Kotlin Android code runs in an app with camera permission granted?
Android Kotlin
class CameraPreviewActivity : AppCompatActivity() { private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val previewView = PreviewView(this) setContentView(previewView) cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ val cameraProvider = cameraProviderFuture.get() val preview = Preview.Builder().build().also { it.setSurfaceProvider(previewView.surfaceProvider) } val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA cameraProvider.bindToLifecycle(this, cameraSelector, preview) }, ContextCompat.getMainExecutor(this)) } }
Attempts:
2 left
💡 Hint
Think about what PreviewView and bindToLifecycle do in CameraX.
✗ Incorrect
The code sets up a PreviewView and binds the back camera preview to it using CameraX. This shows a live camera feed filling the screen.
❓ lifecycle
intermediate1:30remaining
Camera Lifecycle Binding
What happens if you bind the camera to a lifecycle owner that is already destroyed?
Attempts:
2 left
💡 Hint
Consider how CameraX manages lifecycle states.
✗ Incorrect
Binding to a destroyed lifecycle owner causes CameraX to throw an IllegalStateException because it cannot manage camera resources properly.
📝 Syntax
advanced2:00remaining
Correct Permission Request Code
Which Kotlin code snippet correctly requests camera permission at runtime in an Android app?
Android Kotlin
val CAMERA_PERMISSION = android.Manifest.permission.CAMERA
fun requestCameraPermission(activity: Activity) {
if (ContextCompat.checkSelfPermission(activity, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, arrayOf(CAMERA_PERMISSION), 100)
}
}Attempts:
2 left
💡 Hint
Check the condition for when to request permission.
✗ Incorrect
You request permission only if it is NOT already granted. Option A correctly checks for not granted and then requests permission.
🔧 Debug
advanced2:30remaining
Camera Preview Not Showing
Why does this code fail to show the camera preview even though permissions are granted?
Android Kotlin
class MainActivity : AppCompatActivity() { private lateinit var previewView: PreviewView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) previewView = PreviewView(this) setContentView(previewView) val cameraProviderFuture = ProcessCameraProvider.getInstance(this) cameraProviderFuture.addListener({ val cameraProvider = cameraProviderFuture.get() val preview = Preview.Builder().build() val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA preview.setSurfaceProvider(previewView.surfaceProvider) cameraProvider.bindToLifecycle(this, cameraSelector, preview) }, ContextCompat.getMainExecutor(this)) } }
Attempts:
2 left
💡 Hint
Check the order of setting the surface provider and binding the preview.
✗ Incorrect
The surface provider must be set before binding the preview to lifecycle. Calling setSurfaceProvider after bindToLifecycle means the preview has no surface to display on when bound.
🧠 Conceptual
expert2:00remaining
CameraX Use Cases
Which CameraX use case should you use to capture a high-quality still photo with automatic focus and flash support?
Attempts:
2 left
💡 Hint
Think about which use case is designed for still photo capture.
✗ Incorrect
ImageCapture use case is designed for taking high-quality photos with features like autofocus and flash. Preview only shows live feed, ImageAnalysis is for processing frames, VideoCapture is for videos.