Bird
Raised Fist0
Android Kotlinmobile~20 mins

Camera access in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using MediaStore.ACTION_IMAGE_CAPTURE in an Android app?
easy
A. To open the device camera to take a photo
B. To open the device gallery to select a photo
C. To record audio using the microphone
D. To access device location services

Solution

  1. Step 1: Understand the intent action

    MediaStore.ACTION_IMAGE_CAPTURE is a predefined intent action to launch the camera app for capturing images.
  2. 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.
  3. Final Answer:

    To open the device camera to take a photo -> Option A
  4. Quick Check:

    Camera intent = open camera [OK]
Hint: Remember: ACTION_IMAGE_CAPTURE means open camera [OK]
Common Mistakes:
  • Confusing camera intent with gallery intent
  • Thinking it records audio
  • Assuming it accesses location
2. Which of the following is the correct Kotlin syntax to start the camera intent for result in an Activity?
easy
A. startActivityForResult(Intent(MediaStore.ACTION_IMAGE_CAPTURE), REQUEST_CODE)
B. startActivity(Intent(MediaStore.ACTION_IMAGE_CAPTURE))
C. startActivityForResult(MediaStore.ACTION_IMAGE_CAPTURE, REQUEST_CODE)
D. startActivityForResult(Intent(REQUEST_CODE), MediaStore.ACTION_IMAGE_CAPTURE)

Solution

  1. Step 1: Check the method signature

    startActivityForResult requires an Intent and a request code integer.
  2. Step 2: Verify the Intent creation

    The Intent must be created with MediaStore.ACTION_IMAGE_CAPTURE as the action.
  3. Final Answer:

    startActivityForResult(Intent(MediaStore.ACTION_IMAGE_CAPTURE), REQUEST_CODE) -> Option A
  4. Quick Check:

    Intent + request code = startActivityForResult [OK]
Hint: Intent first, then request code in startActivityForResult [OK]
Common Mistakes:
  • Passing action string instead of Intent
  • Omitting request code
  • Swapping Intent and request code parameters
3. Given this code snippet in an Activity, what will be the output when the user takes a photo successfully?
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)
  }
}
medium
A. The app will display a default image instead
B. The app will crash due to a null pointer exception
C. Nothing happens because onActivityResult is not called
D. The photo taken by the user will be displayed in imageView

Solution

  1. Step 1: Understand onActivityResult behavior

    This method receives the result from the camera intent when the user takes a photo.
  2. 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.
  3. Final Answer:

    The photo taken by the user will be displayed in imageView -> Option D
  4. Quick Check:

    Photo bitmap set to imageView = display photo [OK]
Hint: Check requestCode and resultCode before using data [OK]
Common Mistakes:
  • Not checking requestCode or resultCode
  • Casting data extras incorrectly
  • Forgetting to call super.onActivityResult
4. Identify the error in this Kotlin code snippet for launching the camera:
val intent = Intent()
intent.action = MediaStore.ACTION_IMAGE_CAPTURE
startActivityForResult(intent)
medium
A. Intent action should be set using setAction() method
B. Missing request code parameter in startActivityForResult
C. Intent should be created with MediaStore.ACTION_IMAGE_CAPTURE in constructor
D. startActivityForResult cannot be used to launch camera

Solution

  1. Step 1: Check startActivityForResult usage

    This method requires two parameters: an Intent and a request code integer.
  2. Step 2: Identify missing parameter

    The code calls startActivityForResult with only the Intent, missing the request code.
  3. Final Answer:

    Missing request code parameter in startActivityForResult -> Option B
  4. Quick Check:

    startActivityForResult needs Intent + request code [OK]
Hint: Always pass request code with Intent in startActivityForResult [OK]
Common Mistakes:
  • Omitting request code parameter
  • Using wrong Intent constructor
  • Misunderstanding startActivityForResult signature
5. You want to save a full-size photo taken by the camera to a file in your app. Which approach correctly combines camera intent and file handling in Kotlin?
hard
A. Save photo by reading camera app cache directory after intent finishes
B. Start camera intent without extras, then save thumbnail from data extras
C. Create a file URI, pass it with Intent extra MediaStore.EXTRA_OUTPUT, then start camera intent
D. Use MediaStore.ACTION_VIDEO_CAPTURE intent to save photo file

Solution

  1. 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.
  2. Step 2: Use Intent extra MediaStore.EXTRA_OUTPUT

    Pass the file URI as an extra in the camera intent using MediaStore.EXTRA_OUTPUT before starting the intent.
  3. 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.
  4. Final Answer:

    Create a file URI, pass it with Intent extra MediaStore.EXTRA_OUTPUT, then start camera intent -> Option C
  5. Quick Check:

    File URI + EXTRA_OUTPUT = full photo saved [OK]
Hint: Use EXTRA_OUTPUT with file URI for full-size photos [OK]
Common Mistakes:
  • Relying on thumbnail from data extras only
  • Not providing file URI to camera intent
  • Using video capture intent for photos
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard