I’m working through the book, and encountered the following issue. When I attempt to enable/disable the crimeCamera button, I use the code highlighted in the middle of page 357:
val captureImageIntent = takePhoto.contract.createIntent(requireContext(), null) crimeCamera.isEnabled = canResolveIntent(captureImageIntent)
When run, my program throws an exception:
java.lang.NullPointerException: Parameter specified as non-null is null
… and the trace identifies the first line above as the culprit. I was unable to find the issue debugging, and after commenting out the enable/disable code, I was able to complete the rest of the chapter without a hitch: the camera works, the image is stored, the thumbnail is displayed, etc.
Any advice for a workaround? I’d still like this code to work if it is possible.
Since TakePicture() contract requires a Uri for saving pictures it is better to use the next approach:
val canTakePhoto = Intent(MediaStore.ACTION_IMAGE_CAPTURE).resolveActivity(requireContext().packageManager) != null
crimeCamera.isEnabled = canTakePhoto
This code checks whether your device can process an action ACTION_IMAGE_CAPTURE and doesn’t depend on TakePicture limitations.