问题描述
我有一个基本的 :app 模块。还有 :camera 模块。
在相机模块中,我使用 Intent 打开本机相机。
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);
}