Android 7.0 调取相机崩溃 android.os.FileUriExposedException

来源:互联网 发布:ubuntu smbpassword 编辑:程序博客网 时间:2024/06/04 01:26

/**
* 选择相机
*/
private void showCameraAction() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// 设置系统相机拍照后的输出路径
mTmpFile = FileUtil.createTmpFile(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
doTakePhotoIn7(mTmpFile.getAbsolutePath());
} else {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mTmpFile));
startActivityForResult(cameraIntent,
CameraSdkParameterInfo.TAKE_PICTURE_FROM_CAMERA);
}

    } else {        Toast.makeText(this, R.string.camerasdk_msg_no_camera,                Toast.LENGTH_SHORT).show();    }}//在Android7.0以上拍照private void doTakePhotoIn7(String path) {    Uri mCameraTempUri;    try {        ContentValues values = new ContentValues(1);        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");        values.put(MediaStore.Images.Media.DATA, path);        mCameraTempUri = getContentResolver()                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);        takePhoto(CameraSdkParameterInfo.TAKE_PICTURE_FROM_CAMERA, mCameraTempUri);    } catch (Exception e) {        e.printStackTrace();    }}public  void takePhoto(int requestCode, Uri uri) {    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);    if (uri != null) {        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);    }    startActivityForResult(intent, requestCode);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    // 相机拍照完成后,返回图片路径    if (requestCode == CameraSdkParameterInfo.TAKE_PICTURE_FROM_CAMERA) {        if (resultCode == Activity.RESULT_OK) {            mTmpFile  就是你的拍好照之后的文件 .......            //注意 提前命名的file 此时在这里 data为空 不要通过data在获取你的file路径        }    }}
阅读全文
0 0