android 7.0 调用系统相机崩溃的解决方案(非谷歌官方推荐)

解决方案:

1、(推荐)7.0之后你的app就算有权限,给出一个URI之后手机也认为你没有权限。

不用修改原有代码,在Application的oncreate方法中:(或者直接放在调用相机的activity的onCreate方法中)

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
     StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
     StrictMode.setVmPolicy(builder.build());
   }

 2、(强烈不推荐)在调用相机的时候添加7.0系统的判断(谷歌官方推荐的,但是本人强烈不推荐,坑太多)

/*获取当前系统的android版本号*/
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Log.e("currentapiVersion","currentapiVersion====>"+currentapiVersion);
if (currentapiVersion<24){
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(pathFile));
    startActivityForResult(intent, TAKE_PICTURE);
}else {
    ContentValues contentValues = new ContentValues(1);
    contentValues.put(MediaStore.Images.Media.DATA, pathFile.getAbsolutePath());
    Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(intent, TAKE_PICTURE);
}

推荐使用第一种。

原文地址:https://www.cnblogs.com/1925yiyi/p/10319489.html