Complete the code to check if the app has the CAMERA permission.
val hasCameraPermission = ContextCompat.checkSelfPermission(this, [1]) == PackageManager.PERMISSION_GRANTEDWe check for the CAMERA permission using Manifest.permission.CAMERA.
Complete the code to request CAMERA permission from the user.
ActivityCompat.requestPermissions(this, arrayOf([1]), REQUEST_CAMERA_CODE)We request the CAMERA permission by passing Manifest.permission.CAMERA inside the array.
Fix the error in the permission result check inside onRequestPermissionsResult.
if (requestCode == REQUEST_CAMERA_CODE && grantResults.isNotEmpty() && grantResults[[1]] == PackageManager.PERMISSION_GRANTED) { // Permission granted }
The first permission result is at index 0 in grantResults.
Fill both blanks to create a function that checks and requests CAMERA permission if not granted.
fun checkCameraPermission() {
if (ContextCompat.checkSelfPermission(this, [1]) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf([2]), REQUEST_CAMERA_CODE)
}
}Both blanks require Manifest.permission.CAMERA to check and request the camera permission.
Fill all three blanks to handle permission result and show a message if permission is denied.
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CAMERA_CODE) {
if (grantResults.isNotEmpty() && grantResults[[1]] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, [2], Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, [3], Toast.LENGTH_SHORT).show()
}
}
}Index 0 checks the first permission result. Show a granted message if permission is allowed, else show denied message.