Share content: Permission Denial when using Intent.createChooser

Viewed 299

I am currently adapting my app to Android 11. When I want to share a file, I get the following error message in logcat:

Permission Denial: reading androidx.core.content.FileProvider uri content://... from pid=20333, uid=1000 requires the provider be exported, or grantUriPermission()

But my app is still working and sharing works without problems.

The problem only occurs when I use:

startActivity(Intent.createChooser(intent, "share"));

It does not occur when I use:

startActivity(intent);

In the AndroidManifest.xml of course I set android:requestLegacyExternalStorage to true. targetSdkVersion is 29.

Uri uri = FileProvider.getUriForFile(this, AUTHORITY_FILE, file);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(Intent.createChooser(intent, "share")); // error
startActivity(intent); // no error
0 Answers
Related