3

I am having an unusual issue when dealing with using the camera app with an implicit intent in Android 10. I am using the Big Nerd Ranch Android Programming textbook (4th Edition) chapter 16 to learn how to take pictures and store them in an app. The book walks through the process of setting up a FileProvider, granting the camera app permission to write to a specific URI, and then launching the default camera app using an implicit intent from MediaStore. After following the instructions in the book to the letter, I launched the app on an emulator (Pixel 3XL, Android 10, API 29). When I click the camera button in my app, I get this error:

2020-06-09 23:53:23.092 5894-5894/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.camera2, PID: 5894
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.camera2/com.android.camera.CaptureActivity}: java.lang.NullPointerException: Attempt to get length of null array
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.NullPointerException: Attempt to get length of null array
    at com.android.camera.CameraActivity.shouldUseNoOpLocation(CameraActivity.java:1753)
    at com.android.camera.CameraActivity.onCreateTasks(CameraActivity.java:1438)
    at com.android.camera.util.QuickActivity.onCreate(QuickActivity.java:114)
    at android.app.Activity.performCreate(Activity.java:7802)
    at android.app.Activity.performCreate(Activity.java:7791)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

Upon googling this issue, I found this other StackOverflow post: Camera crashing on Android 10 when launching intent. A response in that post indicated that placing the camera permission in the manifest, and requesting the permission at runtime is necessary to fix the issue. The book argues, however, that this is not necessary. The book's approach does not actually use the camera hardware directly, but rather launches the built-in camera app and tells it to store the output image in a specific location. I would be willing to try to simply add the permissions code, but this is where it gets weird...

1) I have a physical Pixel 3 XL running Android 10, API 29. The exact same code works fine on the physical device.

2) Running the exact same code on a Pixel 3 XL emulator with Android 9, API 28 works fine.

3) I created a different app and used the exact same approach outlined in the book to take pictures in that app. The app works fine in both the Android 9 and 10 emulators, as well as my physical device.

At this point, I don't want to change the approach I use in the sample app from the book, because I have seen this same code work elsewhere. What is causing this? What could be making everything work fine on my physical device, but not on the emulator? What could be making the same approach work in one app on the emulator, but not in a different app on the same emulator? Is there some dependency I am using in the second app that is inadvertently making this picture taking work? (The second app is more complicated that the app listed in the book, so it has quite a few more dependencies)

Any advice is greatly appreciated!

  • No, you do not need the `CAMERA` permission to take a photo like that, and AFAIK, that has not changed with 10. (However, if you list that permission in the manifest, you must also request it at runtime beforehand; otherwise you'll get a `SecurityException`. Apparently, this was done so as not to confuse end users who might manually disable that permission for your app.) The given Exception would seem to be more indicative of a storage permissions issue, rather than the camera. Please [edit] your question to provide the complete stack trace; i.e., all the red lines around the one you've posted – Mike M. Jun 10 '20 at 05:42
  • @MikeM. Thanks for your response! I have posted the entire stack trace. – Austin Promenschenkel Jun 10 '20 at 05:55
  • 2
    OK, long story short, that appears to be a bug in the camera app that has been recently corrected. It's crashing when checking to see if your app has the `ACCESS_FINE_LOCATION` permission for EXIF metadata stuff, and your app apparently doesn't have any `` elements listed in your manifest. Basically, wait for an update to the emulator, or – if you really, really want to avoid this possibility in the wild – you could add some ``; doesn't even have to be real. That's a litte hacky, though. I'd probably rely on the update, unless you needed some permission anyway – Mike M. Jun 10 '20 at 06:06
  • @MikeM. Thank you so much for your detailed response! I went ahead and added the INTERNET permission to my manifest, just for fun, and it worked. It looks like there is an update available for my Android Studio, so I might try that as well, and see if they have fixed it. I am fairly new to the Android development world, so I thought I'd ask: how did you know about this weird bug? I am eager to improve my Android debugging skills, and I'm pretty sure I wouldn't have come up with that myself.... Thanks agaiin! – Austin Promenschenkel Jun 10 '20 at 06:17
  • No problem! Glad that worked. For this one, I basically just read through the stack trace, and checked the source code. The last `Caused by` section is often the most relevant, and there we see the Exception is thrown in `com.android.camera.CameraActivity.shouldUseNoOpLocation()`. There's really only one place in that method that could cause that, but there's a null check first. Looking at the history, though, shows that the check was added relatively recently, and given that all the line numbers match up exactly, I was pretty confident that the version you're running is just a little... – Mike M. Jun 10 '20 at 06:29
  • ...behind the current source. You can kinda see it all at once here: https://android.googlesource.com/platform/packages/apps/Camera2/+blame/master/src/com/android/camera/CameraActivity.java#1739. `packageInfo.requestedPermissions` there is a `String[]` of all of the ``s you have, and when you had none, that was null. – Mike M. Jun 10 '20 at 06:29
  • @MikeM. Makes sense. I guess I just looked at that stack trace and didn't understand anything other than the actual exception itself! I did just complete my android studio update, which included an update to the emulator, and the problem persists... Hopefully next time. Thanks again! – Austin Promenschenkel Jun 10 '20 at 06:37
  • Hello @MikeM, I have gone contents shared by you it is quit useful. I am facing same problem in 'Nexus One API 29' Emulator alike 'Austin Promenschenkel'. Although I didn't faced any problem with 'Pixel 3XL'. My concern is, Is there any chances that we might face same kind of problem with any of Physical Device ? If yes then how we should deal with it. – Chanchal Shakti Aug 04 '21 at 09:28
  • @ChanchalShakti Sure, there's always the possibility of bugs in the device software. I don't really have much in the way of general advice, however, sorry. The bugs themselves have to be fixed by the manufacturer/provider eventually. If you can pin down the problem like we did here, then you could certainly deploy a fix, at least temporarily, if possible. I would imagine most such errors aren't as easy to figure out as this one, though, since everything was open-source here. – Mike M. Aug 04 '21 at 15:27

1 Answers1

0

I have gone contents shared by you it is quit useful. I am facing same problem in 'Nexus One API 29' Emulator alike @Austin Promenschenkel. Although I didn't faced any problem with 'Pixel 3XL'. My concern is, Is there any chances that we might face same kind of problem with any of Physical Device ? If yes then how we should deal with it.

Chanchal Shakti
  • 153
  • 1
  • 8