我有两个申请.我正在尝试使用FileProvider将应用程序A中的文件共享到应用程序B. 应用程序A在应用程序B中的ContentProvider上调用insert方法以插入记录.插入的数据包括我想要从App A共享的文件的Uri.App B中的ContentProvider将尝试从App A读取共享文件.因为我没有使用Intent来共享文件,所以我是Context.grantUriPermission在App A中调用以允许读取(并且有时写入):
mContext.grantUriPermission(MyPackageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)
但是,执行此行会给我(名称已更改以保护无辜者):
java.lang.SecurityException: Uid 10066 does not have permission to uri content://au.com.example.AppA.fileprovider/MyFolder/MyFileName
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:2374)
at android.app.ContextImpl.grantUriPermission(ContextImpl.java:1371)
at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:400)
at etc...
Run Code Online (Sandbox Code Playgroud)
应用程序A在清单文件中具有以下内容:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="au.com.example.AppA.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:readPermission="au.com.example.READ_CONTENT"
android:writePermission="au.com.example.WRITE_CONTENT" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
filepaths.xml有:
<paths>
<files-path
name="MyFolder"
path="MyFolder/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
App A和App B都有以下内容:
<uses-permission android:name="au.com.example.READ_CONTENT" />
<uses-permission android:name="au.com.example.WRITE_CONTENT" />
Run Code Online (Sandbox Code Playgroud)
我已经尝试在两个应用中定义权限.它们都使用相同的调试签名进行签名:
<permission
android:name="au.com.example.READ_CONTENT"
android:permissionGroup="MyGroup"
android:protectionLevel="signature" >
</permission>
<permission
android:name="au.com.example.WRITE_CONTENT"
android:permissionGroup="MyGroup"
android:protectionLevel="signature" >
</permission>
Run Code Online (Sandbox Code Playgroud)
文件最终的实际路径是: …
我已经审阅了几篇关于此问题的帖子,并且尝试了六种建议的解决方案。我有一些与此工作几乎相同的东西,而不会在我编写的另一个应用程序中引发异常,但我只是无法让它不引发异常,尽管文件确实被传输了!正在设置权限并且文件传输得很好,但仍然会抛出异常,尽管它不会使应用程序崩溃。
我所拥有的是这样的:
fun shareVideo(videoFile: File, context: Context) {
if(videoFile.exists()) {
Timber.i("Video file exists and the length in bytes is: ${videoFile.length()}")
} else {
Timber.w("Video file does not exist. Exiting.")
return
}
val uris = arrayListOf<Uri>()
val uri = FileProvider.getUriForFile(context.applicationContext, context.packageName + ".provider", videoFile)
Timber.i("Uri: $uri + path: ${uri.path}")
uris.add(uri)
val intent = Intent()
intent.action = Intent.ACTION_SEND_MULTIPLE
intent.putExtra(Intent.EXTRA_SUBJECT, "Shared files")
intent.type = "video/mp4"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Timber.i("Intent: $intent")
try{
ContextCompat.startActivity(context, Intent.createChooser(intent, "Shared Videos"), null)
} catch (e: Exception) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将位图从我的应用程序的缓存目录发送到短信应用程序。我正在使用文件提供程序向处理意图的应用程序授予临时权限。当我尝试发送意图并从意图选择器中选择默认的 android 短信应用程序时,我的消息应用程序崩溃并出现此错误。我尝试从意图选择器中选择其他应用程序,例如电子邮件和其他消息传递应用程序,它似乎工作正常,只是在使用默认文本消息传递应用程序时崩溃。
java.lang.SecurityException: Permission Denial: reading android.support.v4.content.FileProvider uri content://com.example.brandon.emojimms2/shared_images/image.png from pid=9804, uid=10024 requires the provider be exported, or grantUriPermission()
Run Code Online (Sandbox Code Playgroud)
private void shareImage()
{
File imagePath = new File(mContext.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(mContext, "com.example.brandon.emojimms2", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, mContext.getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
}
Run Code Online (Sandbox Code Playgroud)
我相当确定我正确设置了文件提供程序,但这里是清单,以防万一。 
编辑:我刚刚做了一些测试,似乎短信应用程序崩溃发生在早期 api 的手机上,但正在处理新的 api,例如 …
android android-intent android-contentprovider android-permissions android-fileprovider