【Android笔记】Android 7.0 FileUriExposedException完美解决

【1】问题原因


【2】解决方案

参考链接:

http://www.jianshu.com/p/68a4e8132fcd

http://blog.csdn.net/qq_35001400/article/details/72866770
http://www.cnblogs.com/yongdaimi/p/6067319.html
http://www.jianshu.com/p/9f1dbf6a3114
https://bugly.qq.com/v2/crash-reporting/crashes/6e91d75409/82?pid=1

【2.1】


【2.2】


【2.3】

photopicker图片源代码更改
【1】ImageCaptureManager.java 62 72

/*
    public Intent dispatchTakePictureIntent() throws IOException {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = createImageFile();
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
            }
        }
        return takePictureIntent;
    }


    public void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        mContext.sendBroadcast(mediaScanIntent);
    }
*/
    /* longhui20171107 兼容Android7运行Uri.fromFile修改*/
    public Intent dispatchTakePictureIntent() throws IOException {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uri;
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(mContext.getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = createImageFile();
            // Continue only if the File was successfully created
            if (photoFile != null) {
                /* longhui20171107 兼容Android7运行Uri.fromFile修改,SDK大于24就出错*/
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){//24
                    uri = FileProvider.getUriForFile(mContext,"com.ueh.hcdriver.fileprovider",photoFile);
                }else {
                    uri = Uri.fromFile(photoFile);
                }
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

            }
        }
        return takePictureIntent;
    }


    public void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri;
        /* longhui20171107 兼容Android7运行Uri.fromFile修改,SDK大于24就出错*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){//24
            contentUri = FileProvider.getUriForFile(mContext,"com.ueh.hcdriver.fileprovider",f);
        }else {
            contentUri = Uri.fromFile(f);
        }
        mediaScanIntent.setData(contentUri);
        mContext.sendBroadcast(mediaScanIntent);
    }



【2】photopagerAdapter.java 56行

    final String path = paths.get(position);
    final Uri uri;
    if (path.startsWith("http")) {
      uri = Uri.parse(path);
    } else {
      /* longhui20171107 兼容Android7运行Uri.fromFile修改,SDK大于24就出错*/
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){//24
        uri = FileProvider.getUriForFile(mContext,"com.ueh.hcdriver.fileprovider",new File(path));
      }else {
        uri = Uri.fromFile(new File(path));
      }
    }






评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值