解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/

http://www.jb51.net/article/104948.htm

http://blog.csdn.net/mr_orange_klj/article/details/69660225

http://blog.csdn.net/honjane/article/details/54411820


前言

Android 7.0系统发布后,拿到能升级的nexus 6P,就开始了7.0的适配。发现在Android 7.0以上,在相机拍照和图片裁剪上,可能会碰到以下一些错误:

?
1
2
3
4
5
6
7
Process: com.yuyh.imgsel, PID: 22995
 
// 错误1
android.os.FileUriExposedException: file: ///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()
 
// 错误2
android.os.FileUriExposedException: file: ///storage/emulated/0/DCIM/RxGalleryFinal/IMG_20161018180127.jpg exposed beyond app through Intent.getData()

主要是由于在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。

具体转换方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
  * 转换 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 ;
   }
  }
}

那么,我们在裁剪的时候,应该如下调用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private void crop(String imagePath) {
  File file = new File( "xxx.jpg" );
  cropImagePath = file.getAbsolutePath();
 
  Intent intent = new Intent( "com.android.camera.action.CROP" );
  intent.setDataAndType(getImageContentUri( new File(imagePath)), "image/*" );
  intent.putExtra( "crop" , "true" );
  intent.putExtra( "aspectX" , config.aspectX);
  intent.putExtra( "aspectY" , config.aspectY);
  intent.putExtra( "outputX" , config.outputX);
  intent.putExtra( "outputY" , config.outputY);
  intent.putExtra( "scale" , true );
  intent.putExtra( "return-data" , false );
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
  intent.putExtra( "outputFormat" , Bitmap.CompressFormat.JPEG.toString());
  intent.putExtra( "noFaceDetection" , true );
  startActivityForResult(intent, IMAGE_CROP_CODE);
}

这样就解决了裁剪的问题,但是!!拍照的时候就会出现以下错误:

?
1
2
3
4
5
6
7
8
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null ) {
  tempFile = new File(FileUtils.createRootPath(getActivity()) + "/" + System.currentTimeMillis() + ".jpg" );
  LogUtils.e(tempFile.getAbsolutePath());
  FileUtils.createFile(tempFile);
  cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
  startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
?
1
android.os.FileUriExposedException: file: ///storage/emulated/0/Android/data/com.yuyh.imgsel/cache/1486438962645.jpg exposed beyond app through ClipData.Item.getUri()

这是因为拍照存储的文件,也需要以Content Uri的形式,故采用以下办法解决:

Step.1

修改AndroidManifest.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
  ...>
 
 
   android:name= "android.support.v4.content.FileProvider"
   android:authorities= "{替换为你的包名}.provider"
   android:exported= "false"
   android:grantUriPermissions= "true" >
  
    android:name= "android.support.FILE_PROVIDER_PATHS"
    android:resource= "@xml/provider_paths" />
 

Step.2

在res/xml/下新建provider_paths.xml文件

?
1
2
3
4
"1.0" encoding= "utf-8" ?>
"http://schemas.android.com/apk/res/android" >
  "external_files" path= "." />

Step.3

修改拍照时的参数

?
1
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(getActivity(),BuildConfig.APPLICATION_ID + ".provider" , tempFile)); //Uri.fromFile(tempFile)

总结

好了,以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流。

原文链接:http://blog.csdn.net/yyh352091626/article/details/54908624

1、在AndroidManifest.xml中添加如下代码

复制代码
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    ="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        provider>
    application>
manifest>
复制代码

2、在res目录下新建一个xml文件夹,并且新建一个provider_paths的xml文件

xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
paths>
注意 external-path代表了不同的文件目录,一共有五种,下面是官网的说明:
 name="name" path="path" /> 
Represents files in the  files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir(). 代表了 Context.getFilesDir() .
 name="name" path="path" />
Represents files in the cache subdirectory of your app's internal storage area. The root path of this subdirectory is the same as the value returned by  getCacheDir().
 name="name" path="path" />
Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned by Environment.getExternalStorageDirectory().
 name="name" path="path" />
Represents files in the root of your app's external storage area. The root path of this subdirectory is the same as the value returned by Context#getExternalFilesDir(String) Context.getExternalFilesDir(null).
 name="name" path="path" />
Represents files in the root of your app's external cache area. The root path of this subdirectory is the same as the value returned by Context.getExternalCacheDir().

3、修改代码

Uri photoURI = Uri.fromFile(createImageFile());

变成:

Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());

 




解决android N文件访问crash android.os.FileUriExposedException file:///storage/emulated/0/xxx

原因:

Android N对访问文件权限收回,按照Android N的要求,若要在应用间共享文件,您应发送一项 content://URI,并授予 URI 临时访问权限。 
而进行此授权的最简单方式是使用 FileProvider类。

解决方法:

1.在mainfest中加入FileProvider注册

<application>
 ......
     <provider
         android:authorities="你的应用名.fileprovider"
         android:name="android.support.v4.content.FileProvider"
         android:grantUriPermissions="true"
         android:exported="false">
         <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
               android:resource="@xml/filepaths"/>
    provider>

application>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13


2.配置filepaths文件


<paths>
    <external-path path="honjane/" name="files_path" />
paths>
  • 1
  • 2
  • 3
  • 4

其中: 
files-path代表的根目录: Context.getFilesDir() 
external-path代表的根目录: Environment.getExternalStorageDirectory() 
cache-path代表的根目录: getCacheDir()

<external-path path="honjane/" name="files_path" />
  • 1

path 代表要共享的目录 
name 只是一个标示,随便取吧 自己看的懂就ok

举个栗子:通过provider获取到的uri链接

content://com.honjane.providerdemo.fileprovider/files_path/files/b7d4b092822da.pdf

name对应到链接中的files_path

path对应到链接中的 files ,当然files是在honjane/目录下

3.访问文件

 /**
     * 打开文件
     * 当手机中没有一个app可以打开file时会抛ActivityNotFoundException
     * @param context     activity
     * @param file        File
     * @param contentType 文件类型如:文本(text/html)     
     */
    public static void startActionFile(Context context, File file, String contentType) throws ActivityNotFoundException {
        if (context == null) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setDataAndType(getUriForFile(context, file), contentType);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 打开相机
     *
     * @param activity    Activity
     * @param file        File
     * @param requestCode result requestCode
     */
    public static void startActionCapture(Activity activity, File file, int requestCode) {
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, getUriForFile(activity, file));
        activity.startActivityForResult(intent, requestCode);
    }

    private static Uri getUriForFile(Context context, File file) {
        if (context == null || file == null) {
            throw new NullPointerException();
        }
        Uri uri;
        if (Build.VERSION.SDK_INT >= 24) {
            uri = FileProvider.getUriForFile(context.getApplicationContext(), "你的应用名.fileprovider", file);
        } else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

同样访问相机相册都通过FileProvider.getUriForFile申请临时共享空间 
已写成工具类上传到github,需要直接下载 
使用方法简单,一行代码搞定 
打开文件:

 try {
      FileUtils.startActionFile(this,path,mContentType);
    }catch (ActivityNotFoundException e){

 }
  • 1
  • 2
  • 3
  • 4
  • 5

调用相机:

 FileUtils.startActionCapture(this, file, requestCode);
  • 1

修复bug:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/xxx/xxx/file/12b31d2cab6ed.pdf

external与storage/emulated/0/对应,乍一看貌似没什么问题,path设置的是external的根路径,对应Environment.getExternalStorageDirectory(),

然而这个方法所获取的只是内置SD卡的路径,所以当选择的相册中的图片是外置SD卡的时候,就查找不到图片地址了,因此便抛出了failed to find configured root that contains的错误。

通过分析FileProvider源码发现,在xml解析到对应的标签后,会执行 buildPath() 方法来将根标签(files-path,cache-path,external-path等)对应的路径作为文件根路径,

在buildPath(),会根据一些常量判断是构建哪个目录下的path,除了上面介绍的几种path外还有个TAG_ROOT_PATH = “root-path” ,只有当不是root-path时才会去构建其他path,

官方也没介绍这个root-path,测试了一下发现对应的是DEVICE_ROOT指向的整个存储的根路径,这个bug就修复了

修改filepaths文件:

<paths>
      <root-path name="honjane" path="" />
paths>
  • 1
  • 2
  • 3

代码下载:https://github.com/honjane/fileProviderDemo




你可能感兴趣的:(解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/)