Android7.0及以上打开相机闪退,startActivityForResult报错解决

这个问题出现的原因主要是由于在Android 7.0以后,用了Content Uri 替换了原本的File Uri,故在targetSdkVersion=24的时候,部分 “`Uri.fromFile()“` 方法就不适用了。 **File Uri 与 Content Uri 的区别** - File Uri 对应的是文件本身的存储路径 - Content Uri 对应的是文件在Content Provider的路径 所以在android 7.0 以上,我们就需要将File Uri转换为 Content Uri。具体转换方法如下:(需要使用的时候直接复制粘贴即可)

 /**
     * 转换 content:// uri
     *
     * @param imageFile
     * @return
     */
    public Uri getImageContentUri(File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Images.Media._ID },
                MediaStore.Images.Media.DATA + "=? ",
                new String[] { filePath }, null);

        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

主要代码就是这样,下面写一个完整的调取系统相机的代码:

   File outputImage = new File(ct.getSDPath() + "/bhne/export/" + projectpath + "/", java.util.UUID.randomUUID().toString() + ".jpg");
                                pathImage = outputImage.toString();
                                try {
                                    if (outputImage.exists()) {
                                        outputImage.delete();
                                    }
                                    outputImage.createNewFile();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                                Intent intentPhoto = new Intent("android.media.action.IMAGE_CAPTURE"); //拍照
                                //Uri imageUri = Uri.fromFile(outputImage);//7.0之前的写法
                                Uri imageUri = getImageContentUri(outputImage);//直接调用咱们上面写好的方法
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                                    intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT,
                                            FileProvider.getUriForFile(PictureActivity.this,"bhne.com.wlprojectapplication.fileprovider", outputImage));
                                }else {
                                    intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outputImage));
                                }






                                intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                                startActivityForResult(intentPhoto, 3);


  Uri imageUri = getImageContentUri(outputImage);//直接调用咱们上面写好的方法

主要就是这一行代码,调用了上面所写的代码,还需注意的是需要在权限中声明一下:

 <application
        ...>


        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="bhne.com.wlprojectapplication.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>

然后在res文件下创建一个xml文件夹,在文件夹下创建新的xml文件,名字为file_paths:


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
基本上就是这样,以后希望长点记性。


猜你喜欢

转载自blog.csdn.net/haojiagou/article/details/80761709