I have 2 apps - Demo and Pro. Demo has a content provider and when Pro is installed it needs to transfer all files from the demo provider.
Demo app (provider):
<provider
android:name="***.provider.InternalStorageProvider"
android:authorities="***.demo.storage.int.provider"
android:exported="false"
android:syncable="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/int_storage_paths" />
</provider>
Pro app (consumer):
Experiment 1:
ParcelFileDescriptor pfd = cr.openFileDescriptor(exposedFileUri, "r");
FileInputStream input = new FileInputStream(pfd.getFileDescriptor());
java.lang.SecurityException: Permission Denial: opening provider .provider.InternalStorageProvider from ProcessRecord{9c85875 10734:/u0a61} (pid=10734, uid=10061) that is not exported from uid 10062
Experiment 2:
Activity activity = getActivity(); activity.grantUriPermission(activity.getPackageName(), exposedFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
ParcelFileDescriptor pfd = cr.openFileDescriptor(exposedFileUri, "r"); FileInputStream input = new FileInputStream(pfd.getFileDescriptor());
java.lang.SecurityException: Uid 10061 does not have permission to uri 0 @ content://***.demo.storage.int.provider/db/file1
InternalStorageProvider
is a copy of a normal FileProvider
. But it doesn't matter as the execution cannot even reach it. Exceptions are thrown before it is called.
Note that no chooser activities and intents are involved. The consumer tries to open the file from a known uri directly, without choosers. Most of the examples I've found are using Intent.FLAG_GRANT_READ_URI_PERMISSION
but I don't use an intent at all.
How I am supposed to grant uri permissions to the consumer correctly?
<grant-uri-permission>Specifies the subsets of app data that the parent content provider has permission to access. Data subsets are indicated by the path part of a content: URI. (The authority part of the URI identifies the content provider.)
androidx.core.content.FileProvider. FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri . A content URI allows you to grant read and write access using temporary access permissions.
You need to give permission to pro app...
activity.grantUriPermission("pro app package name", exposedFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With