android - 打开意图相机时,提供者无权访问内容
问题描述
我有一个基本的 :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"
那么相机就可以工作了。但是由于安装了其他构建变体,该应用程序停止安装在手机上
解决方案
相机权限保护级别是危险的。
所以你必须在运行时询问用户这个权限。
在这里可以看到相机权限等权限级别: 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);
}
推荐阅读
- python - Python 在 Windows CMD 中没有名为“requests_html”的模块
- macos - Mac MySql 上的 XAMPP 访问被拒绝?
- php - Woocommerce - 向整个购物车添加附加费,特定类别除外
- matlab - 如何在Windows系统的MATLAB中停止由系统函数启动的进程?
- c++ - 构造函数模板可以使用构造函数委托吗?
- sql-server - 如何在 SQL Server 中使用不同的有效日期获取已售商品的详细信息
- android - 从 Kotlin Native 访问 Android Keystore (Biometrics)
- c - 如何制作高效的循环链表?
- sql - 如何解决 Oracle Apex 中的“无法使用 DISTINCT、GROUP BY 等从视图中选择 FOR UPDATE”错误?
- assembly - 需要帮助了解如何处理系统调用(int 0x80 等)