Loading

java.lang.SecurityException: Permission Denial: writing android.support.v4.content.FileProvider uri

在使用红米手机拍摄照片时,出现闪退的情况。

 

调用系统相机拍摄照片,使用FileProvider.getUriForFile传入Uri时,报异常

java.lang.SecurityException: Permission Denial: writing android.support.v4.content.FileProvider uri ... 

此时需要调用

grantUriPermission(String toPackage, Uri uri, int modeFlags)

授予Uri的访问权限

 

具体代码如下

    try {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        tempFile = File.createTempFile("head", null, StorageUtils.getImageDir(context));
        Uri imgUri = FileProvider.getUriForFile(context,
                context.getPackageName() + ".FileProvider", tempFile);
        List resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        if (resInfoList.size() == 0) {
            DadaToast.showToast(context, "没有合适的相机应用程序");
            return;
        }
        Iterator resInfoIterator = resInfoList.iterator();
        while (resInfoIterator.hasNext()) {
            ResolveInfo resolveInfo = (ResolveInfo) resInfoIterator.next();
            String packageName = resolveInfo.activityInfo.packageName;
            grantUriPermission(packageName, imgUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        startActivityForResult(intent, REQUEST_TAKE);

    } catch (ActivityNotFoundException e1) {
        DadaToast.showToast(context, "没有合适的相机应用程序");
        e1.printStackTrace();
    } catch (IOException e2) {
        DadaToast.showToast("创建文件失败");
        e2.printStackTrace();
    }

 

posted @ 2017-02-07 14:36  辉灬  阅读(9849)  评论(3编辑  收藏  举报