Android 10 中的相机意图权限 [英] Camera intent permissions in Android 10

查看:50
本文介绍了Android 10 中的相机意图权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 10 中处理使用带有隐式意图的相机应用程序时遇到了一个不寻常的问题.我正在使用 Big Nerd Ranch Android 编程教科书(第 4 版)第 16 章来学习如何拍照并将它们存储在一个应用程序.本书介绍了设置 FileProvider 的过程,授予相机应用写入特定 URI 的权限,然后使用来自 MediaStore 的隐式意图启动默认相机应用.按照书中的说明进行操作后,我在模拟器(Pixel 3XL、Android 10、API 29)上启动了该应用程序.当我单击应用程序中的相机按钮时,出现此错误:

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) 

在谷歌搜索这个问题时,我发现了另一个 StackOverflow 帖子:Camera启动 Intent 时在 Android 10 上崩溃.该帖子中的回复表明,将相机权限放在清单中并在运行时请求权限是解决问题的必要条件.然而,该书认为这没有必要.这本书的方法实际上并没有直接使用相机硬件,而是启动内置相机应用程序并告诉它在特定位置存储输出图像.我愿意尝试简单地添加权限代码,但这就是它变得奇怪的地方...

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) 我有一台运行 Android 10、API 29 的物理 Pixel 3 XL.完全相同的代码在物理设备上运行良好.

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

2) 在搭载 Android 9 的 Pixel 3 XL 模拟器上运行完全相同的代码,API 28 工作正常.

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

3) 我创建了一个不同的应用程序,并使用书中概述的完全相同的方法在该应用程序中拍照.该应用在 Android 9 和 10 模拟器以及我的物理设备上都运行良好.

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)

非常感谢任何建议!

推荐答案

我把你分享的内容都删了,很有用.我在Nexus One API 29"模拟器中面临同样的问题@Austin Promenschenkel.虽然我没有遇到Pixel 3XL"的任何问题.我担心的是,我们是否有可能在任何物理设备上遇到同样的问题?如果是,那么我们应该如何处理.

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.

这篇关于Android 10 中的相机意图权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆