android 7.0 调用系统相机崩溃的解决方案

来源:互联网 发布:穿山甲何仙姑 知乎 编辑:程序博客网 时间:2024/05/22 14:29

项目运行两个礼拜了,相机也在7.0以下运行的完美,突然早上同事拿他的7.0手机给我说 这是一个大bug.我一看调用相机直接崩溃。报的错误如下图:


接着我以为是我的文件路径错误,找了老半天没发现问题,仔细想想不太可能了。于是乎,开始求助各大网友了。

解决方案:

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

不用修改原有代码,在Application的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://developer.android.com/reference/android/support/v4/content/FileProvider.html


阅读全文
1 2
原创粉丝点击