解决方法一:
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(MyApplication.getFileDir(getActivity()), String.valueOf(System.currentTimeMillis()) + ".jpg");
path = file.getPath();
if (Build.VERSION.SDK_INT<24){
Uri imageUri = Uri.fromFile(file);
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
}else{
//兼容android7.0 使用共享文件的形式
ContentValues contentValues = new ContentValues(1);
contentValues.put(MediaStore.Images.Media.DATA, path);
Uri uri = getActivity().getApplication().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
startActivityForResult(openCameraIntent, TAKE_PICTURE);
解决方法二:
//在application 的onCreate里
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
其他方式参考:Android7.0拍照,权限验证,一行代码解决