provider does not have permission to content when open intent camera

Viewed 351

I have a basic :app module. and there is the :camera module. In the camera module, I open the native camera using intent. MediaStore.ACTION_IMAGE_CAPTURE

but the camera doesn't work, as I get an error

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]

here's mine provider AndroidManifest in app:

<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>

if you change the android:authorities on "com.example.android.provider" then the camera works. But the app stops being installed on the phone due to other build variants installed

1 Answers

The Camera permission protected level is dangerous.

so you have to ask the user for this permission in runtime.

here you can see the permissions level such as camera permission: https://developer.android.com/reference/android/Manifest.permission#CAMERA

And look in the Android developer training site examples how exactly you ask this permissions. The all information is their: https://developer.android.com/training/permissions/requesting

The code should look like that:

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);
}
Related