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);

解决方法二:

//在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拍照,权限验证,一行代码解决

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容