0
0
Android Kotlinmobile~20 mins

Camera access in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Camera Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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))
  }
}
AThe app shows a live camera preview from the back camera filling the screen.
BThe app shows a blank white screen with no camera preview.
CThe app crashes with a NullPointerException because previewView is not initialized.
DThe app shows a static image instead of a live camera preview.
Attempts:
2 left
💡 Hint
Think about what PreviewView and bindToLifecycle do in CameraX.
lifecycle
intermediate
1:30remaining
Camera Lifecycle Binding
What happens if you bind the camera to a lifecycle owner that is already destroyed?
AThe camera preview stops immediately and no image is shown.
BThe camera preview continues to run in the background.
CThe app throws an IllegalStateException at runtime.
DThe app silently ignores the binding and shows a blank screen.
Attempts:
2 left
💡 Hint
Consider how CameraX manages lifecycle states.
📝 Syntax
advanced
2: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)
  }
}
A
if (ContextCompat.checkSelfPermission(activity, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
  ActivityCompat.requestPermissions(activity, arrayOf(CAMERA_PERMISSION), 100)
}
B
if (ContextCompat.checkSelfPermission(activity, CAMERA_PERMISSION) == PackageManager.PERMISSION_GRANTED) {
  ActivityCompat.requestPermissions(activity, arrayOf(CAMERA_PERMISSION), 100)
}
CActivityCompat.requestPermissions(activity, arrayOf(CAMERA_PERMISSION), 100)
D
if (ActivityCompat.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.
🔧 Debug
advanced
2: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))
  }
}
AcameraProviderFuture.get() is called on the main thread causing a deadlock.
BpreviewView is not added to the layout, so preview cannot display.
CCamera permission is missing in the manifest, so preview fails silently.
Dpreview.setSurfaceProvider is called after bindToLifecycle, so preview has no surface when bound.
Attempts:
2 left
💡 Hint
Check the order of setting the surface provider and binding the preview.
🧠 Conceptual
expert
2:00remaining
CameraX Use Cases
Which CameraX use case should you use to capture a high-quality still photo with automatic focus and flash support?
APreview use case, because it shows live camera feed and can capture photos.
BImageCapture use case, because it supports high-quality photo capture with focus and flash.
CImageAnalysis use case, because it processes images and saves photos automatically.
DVideoCapture use case, because it can capture both photos and videos with focus.
Attempts:
2 left
💡 Hint
Think about which use case is designed for still photo capture.