首页 > 解决方案 > 打开意图相机时,提供者无权访问内容

问题描述

我有一个基本的 :app 模块。还有 :camera 模块。在相机模块中,我使用意图打开本机相机。 MediaStore.ACTION_IMAGE_CAPTURE

但相机不工作,因为我得到一个错误

UID 10388 does not have permission to content://com.example.android.provider/attachment_file/Android/media/com.example.android.dev/some_name.jpg [user 0]

这是应用程序中的我的供应商 AndroidManifest:

<provider
     android:name="androidx.core.content.FileProvider"
     android:authorities="${applicationId}.provider"
     android:exported="false"
     android:grantUriPermissions="true">
     <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/file_path" />
 </provider>

如果你改变了android:authorities"com.example.android.provider"那么相机就可以工作了。但是由于安装了其他构建变体,该应用程序停止安装在手机上

标签: androidpermission-deniedandroid-fileprovider

解决方案


相机权限保护级别是危险的。

所以你必须在运行时询问用户这个权限。

在这里可以看到相机权限等权限级别: https ://developer.android.com/reference/android/Manifest.permission#CAMERA

并在 Android 开发人员培训网站示例中查看您究竟是如何要求此权限的。所有信息都是他们的: https ://developer.android.com/training/permissions/requesting

代码应如下所示:

if (ContextCompat.checkSelfPermission(
        CONTEXT, Manifest.permission.REQUESTED_PERMISSION) ==
        PackageManager.PERMISSION_GRANTED) {
    // You can use the API that requires the permission.
    performAction(...);
} else if (shouldShowRequestPermissionRationale(...)) {
    // In an educational UI, explain to the user why your app requires this
    // permission for a specific feature to behave as expected. In this UI,
    // include a "cancel" or "no thanks" button that allows the user to
    // continue using your app without granting the permission.
    showInContextUI(...);
} else {
    // You can directly ask for the permission.
    // The registered ActivityResultCallback gets the result of this request.
    requestPermissionLauncher.launch(
            Manifest.permission.REQUESTED_PERMISSION);
}

推荐阅读