0
0
Android Kotlinmobile~10 mins

Camera access in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to request camera permission in an Android app.

Android Kotlin
ActivityCompat.requestPermissions(this, arrayOf([1]), 100)
Drag options to blanks, or click blank then click option'
AManifest.permission.CAMERA
BManifest.permission.ACCESS_FINE_LOCATION
CManifest.permission.READ_CONTACTS
DManifest.permission.RECORD_AUDIO
Attempts:
3 left
💡 Hint
Common Mistakes
Using location or audio permissions instead of camera permission.
Forgetting to request permissions before accessing the camera.
2fill in blank
medium

Complete the code to check if camera permission is granted.

Android Kotlin
if (ContextCompat.checkSelfPermission(this, [1]) == PackageManager.PERMISSION_GRANTED) {
    // Permission granted
}
Drag options to blanks, or click blank then click option'
AManifest.permission.ACCESS_FINE_LOCATION
BManifest.permission.READ_SMS
CManifest.permission.RECORD_AUDIO
DManifest.permission.CAMERA
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong permission strings.
Not comparing the result to PackageManager.PERMISSION_GRANTED.
3fill in blank
hard

Fix the error in the code to open the camera using an Intent.

Android Kotlin
val cameraIntent = Intent([1])
startActivityForResult(cameraIntent, 101)
Drag options to blanks, or click blank then click option'
AMediaStore.ACTION_IMAGE_CAPTURE
BIntent.ACTION_VIEW
CIntent.ACTION_SEND
DMediaStore.ACTION_VIDEO_CAPTURE
Attempts:
3 left
💡 Hint
Common Mistakes
Using ACTION_VIEW or ACTION_SEND which do not open the camera.
Using ACTION_VIDEO_CAPTURE when you want to take photos.
4fill in blank
hard

Fill both blanks to handle the camera permission result in onRequestPermissionsResult.

Android Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == [1]) {
        if (grantResults.isNotEmpty() && grantResults[0] == [2]) {
            // Permission granted
        }
    }
}
Drag options to blanks, or click blank then click option'
A100
B101
CPackageManager.PERMISSION_GRANTED
DPackageManager.PERMISSION_DENIED
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong requestCode in the check.
Comparing grantResults to PERMISSION_DENIED instead of PERMISSION_GRANTED.
5fill in blank
hard

Fill all three blanks to create a camera preview using CameraX in a Fragment.

Android Kotlin
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener({
    val cameraProvider = cameraProviderFuture.get()
    val preview = Preview.Builder().build().also {
        it.setSurfaceProvider(viewFinder.[1])
    }
    val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
    try {
        cameraProvider.unbindAll()
        cameraProvider.bindToLifecycle(this, cameraSelector, preview, [2])
    } catch(exc: Exception) {
        Log.e("CameraX", "Use case binding failed", exc)
    }
}, ContextCompat.getMainExecutor(requireContext()))

val imageCapture = ImageCapture.Builder().build()

// Use imageCapture to take pictures

// The blank [3] should be the variable name for imageCapture
Drag options to blanks, or click blank then click option'
AsurfaceProvider
BimageCapture
Cpreview
DcameraSelector
Attempts:
3 left
💡 Hint
Common Mistakes
Using preview instead of surfaceProvider for setSurfaceProvider.
Passing preview instead of imageCapture to bindToLifecycle.
Confusing variable names for imageCapture.