Android 授予FileProvider的uri权限会导致SecurityException
我有两个应用程序-Demo和Pro。Demo有一个内容提供程序,安装Pro后需要从Demo提供程序传输所有文件 演示应用程序(提供商):Android 授予FileProvider的uri权限会导致SecurityException,android,android-contentprovider,android-permissions,android-securityexception,Android,Android Contentprovider,Android Permissions,Android Securityexception,我有两个应用程序-Demo和Pro。Demo有一个内容提供程序,安装Pro后需要从Demo提供程序传输所有文件 演示应用程序(提供商): <provider android:name="***.provider.InternalStorageProvider" android:authorities="***.demo.storage.int.provider" android:exported="false"
<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应用程序(消费者):
<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>
- 实验1:
ParcelFileDescriptor pfd=cr.openFileDescriptor(exposedFileUri,“r”)
FileInputStream输入=新的FileInputStream(pfd.getFileDescriptor())
- 实验2: Activity=getActivity(); grantUriPermission(activity.getPackageName(),exposedFileUri,Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION) ParcelFileDescriptor pfd=cr.openFileDescriptor(exposedFileUri,“r”); FileInputStream输入=新的FileInputStream(pfd.getFileDescriptor())
InternalStorageProvider
是普通FileProvider
的副本。但这并不重要,因为执行甚至达不到它。异常在调用之前抛出。
请注意,不涉及选择器活动和意图。使用者尝试直接从已知uri打开文件,而不使用选择器。我发现的大多数示例都使用了Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION
,但我根本没有使用Intent
我应该如何正确地向消费者授予uri权限?您需要向pro app授予权限
activity.grantUriPermission("pro app package name", exposedFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
现在讨论这个问题,你有没有可能解决它?那是很久以前的事了,我几乎什么都不记得了。这个东西真的很邪恶。通过将
FLAG\u GRANT\u READ\u URI\u PERMISSION
传递给我的应用程序中使用URI的所有其他意图,解决了这个问题