Permission Denial: Security Exception while sharing file with FileProvider Android 11 phone

3.7k views Asked by At

I am getting below exception on the Android 11 device but able to share those files in the email without any issue.

Java:Lang:SecurityException: Permission Denial:Reading androidx.core.content.FileProvider.uri content://com.example.myapp.fileprovider/external_files_files/images/default_image.jpg from pid= 10847, uid=1000 requires the provider be exported or granUriPermission.

Issue is only coming for Android 11 device when I shared multiple files otherwise single file works fine without any issue when I used Intent.setdata = uri(singal uri object).

My Code:

Manifest File declaration :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
        ...>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>
</manifest>

File Path

<paths>
    <external-files-path
        name="images"
        path="."/>
</paths>

Sharing code.

    ArrayList<Uri> Urilist = new ArrayList<String>();
 
    // Adding multiple files as below.
    File imagePath = new File(Context.getExternalFilesDir(null), "images");
    File newFile = new File(imagePath, "default_image.jpg");
    Uri contentUri = getUriForFile(getContext(), "com.example.myapp.fileprovider", newFile);

    // Logic to add uri here

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent. ACTION_SEND_MULTIPLE);
    shareIntent.putExtra(Intent.EXTRA_STREAM, Urilist);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    shareIntent.setType(*/*);

    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));

Thanks.

1

There are 1 answers

1
ketan muttha On

I fixed this issue by replacing below line.

startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));

with

startActivity(shareIntent);