Android 7.0 调用照相机照相报错 FileUriExposedException 解决

Android 7.0 调用照相机照相报错 FileUriExposedException 解决

 

原文简书地址:https://www.jianshu.com/p/57ba0d1511b9

 

 

    // 启动手机相机拍摄照片作为头像
    private void choseHeadImageFromCameraCapture() {
        Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果是7.0android系统
            mImageCaptureUri=FileProvider.getUriForFile(this.getActivity(), ProviderUtil.getFileProviderName(this.getActivity()), getImagePath(IMAGE_FILE_NAME));
        }else{
            mImageCaptureUri = Uri.fromFile(getImagePath(IMAGE_FILE_NAME));
        }
        // 判断存储卡是否可用,存储照片文件
        if (SDUtil.hasSdcard()) {
            intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
            Log.e("uri",mImageCaptureUri+"");
        }
        this.startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST);
    }

getImagePath方法:

 

 

 private File getImagePath(String imgName) {
        File file = new File(Environment.getExternalStorageDirectory(), imgName);
        return file;
    }

Adnroidmanifest配置:

 

 

 
        
            
        


ImagePickerProvider 代码:

 

 

public class ImagePickerProvider extends FileProvider {
    
}

 

 

 

 

 


 

 

 

 

 

异常补充:

System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
        at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:605)
        at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:579)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)

 

解决方案:

清单文件AndroidManifest.xml处的android:authorities必须跟mActivity.getPackageName() + ".fileprovider"一样

你可能感兴趣的:(项目经验,经验问题)