通过意图拒绝文件提供者的权限 [英] Permission Denial with File Provider through intent

本文介绍了通过意图拒绝文件提供者的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将位图从应用程序的缓存目录发送到文本消息传递应用程序.我正在使用文件提供程序向处理此意图的应用程序授予临时权限.当我尝试发送意图并从意图选择器中选择默认的Android短信应用程序时,我的消息应用程序崩溃了,并且出现此错误.我尝试从意图选择器中选择其他应用程序,例如电子邮件和其他消息传递应用程序,它似乎运行良好,仅在默认的文本消息传递应用程序崩溃.

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.

我刚刚进行了一些测试,似乎短信通讯应用程序崩溃发生在使用较早api的手机上,但正在使用7.1之类的新api.短信应用程序或您应该授予uri读取权限的方式是否发生了变化?

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?

推荐答案

尝试一下:

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

这篇关于通过意图拒绝文件提供者的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆