最近遇到7.0的手机发现之前跳转拍照直接闪退了,网速百度了一下,原来7.0拍照需要兼容android7.0 使用共享文件的形式,接下来我就告诉大家怎么修改了,直接上代码
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotofile = new File(Environment.getExternalStorageDirectory(),
Path.PATH_DOWNLOAD_IMG+json.getString("keyword") + "_" + System.currentTimeMillis() + ".jpg");
//获取系統版本
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < 24) {
// 从文件中创建uri
phoneuri = Uri.fromFile(takePhotofile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, phoneuri);
} else {
//兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, takePhotofile.getAbsolutePath());
//检查是否有存储权限,以免崩溃
if (ContextCompat.checkSelfPermission(webView.context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
Toast.makeText(webView.context, "请开启存储权限", Toast.LENGTH_SHORT).show();
return;
}
phoneuri = ((Activity) webView.getContext()).getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT, phoneuri);
}
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA
((Activity) webView.getContext()).startActivityForResult(intent, REQUEST_TAKE_PHOTO);
然后在onActivityResult获取takePhotofile 文件的路径就可以啦!