Camera intent permissions in Android 10

≯℡__Kan透↙ 提交于 2021-02-09 10:39:20

问题


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!

来源:https://stackoverflow.com/questions/62296554/camera-intent-permissions-in-android-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!