Complete the code to start an activity for result using the new Activity Result API.
val getContent = registerForActivityResult([1]) { uri ->
// Handle the returned Uri
}The GetContent() contract lets you pick content and get a Uri result.
Complete the code to launch the activity to pick an image.
getContent.launch([1])To pick an image, the MIME type should be "image/*".
Fix the error in the callback parameter type for the activity result launcher.
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { [1] ->
// Use the Uri
}The callback receives a Uri? named here as uri.
Fill both blanks to correctly handle the result in the old onActivityResult method.
override fun onActivityResult(requestCode: Int, resultCode: Int, [1]: Intent?) { super.onActivityResult(requestCode, resultCode, [2]) if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) { val uri = data?.data // Use the uri } }
The parameter and the super call both use data as the Intent variable.
Fill all three blanks to create a map of request codes to their string descriptions.
val requestDescriptions = mapOf(
PICK_IMAGE to [1],
PICK_VIDEO to [2],
PICK_AUDIO to [3]
)This map links each request code to a descriptive string for clarity.
