android 7.0调用相机闪退问题

解决方法一:

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);
解决方法二:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
    }
这段代码必须放在 application 的onCreate里


你可能感兴趣的:(android 7.0调用相机闪退问题)