Permission Denial with File Provider through intent

泪湿孤枕 提交于 2020-02-24 07:49:37

问题


I am attempting send a bitmap from the cache directory of my app to the text messaging app. I am using file provider to grant temporary permission to the application that handles the intent. When I try to send the intent and I select the default android text messaging app from the intent chooser my message app crashes and I get this error. I tried selecting other applications from the intent chooser such as email and other messaging apps and it seemed to work fine, only crashes with the default text messaging app.

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()

Here is the code where I share the intent

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"));
    }
}

I am fairly certain I set up the File provider correctly, but here is the manifest in case it is needed.

Edit: I just did some testing and it seems that the crash with the text messaging app is happening on phones on earlier apis, but is working on new apis such as 7.1. Did either the text messaging app or with the way you were supposed to grant uri read permissions change?


回答1:


Try this:

List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}


来源:https://stackoverflow.com/questions/45893294/permission-denial-with-file-provider-through-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!