解决 Android 7.0 调用照相机照相报错

解决代码:

 // 判断存储卡是否可以用,可用进行存储
        String sdStatus = Environment.getExternalStorageState();
        if (sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //创建拍照后的存储路径
            String path = Environment.getExternalStorageDirectory().toString() + "/Photo";
            File path1 = new File(path);
            if (!path1.exists()) {
                path1.mkdirs();
            }
            String imageFileName = System.currentTimeMillis() + ".jpg";
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//如果是7.0android系统
                ContentValues contentValues = new ContentValues(1);
                contentValues.put(MediaStore.Images.Media.DATA, new File(path, imageFileName).getAbsolutePath());
                imageFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
            } else {
                imageFileUri = Uri.fromFile(new File(path, imageFileName));
            }

            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
            startActivityForResult(intent, action);
        }else {
            ToastUtils.showShort("SD卡不可用");
        }

获取拍照后图片地址:

 if (imageFileUri != null) {
                        //根据uri获取图片路径
                        logoImg = BitmapUtil.getRealFilePath(getApplication(), imageFileUri);
                    }

同时不要忘了申请权限

你可能感兴趣的:(安卓小坑)