FileProvider使用以及源码浅析

1. FileProvider的使用

1.1 AndroidManifest.xml中定义

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

1.2 res目录下新建xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <root-path name="rp" path="jiangbin"/>
        <files-path name="fp" path="jiangbin"/>
        <cache-path name="cp" path="jiangbin" />
        <external-path name="ep" path="jiangbin"/>
        <external-files-path name="efp" path="jiangbin" />
        <external-cache-path name="ecp" path="jiangbin" />
    </paths>
</resources>

1.3 用FileProvider.getUriForFile(Context context, String authority, File file)取代Uri.fromFile(File file)

 File file = new File(mContext.getFilesDir() + "/jiangbin", "hello.txt");
        Uri data;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            data = FileProvider.getUriForFile(mContext, "com.ysx.fileproviderserver.fileprovider", file);
        } else {
            data = Uri.fromFile(file);
        }

1.4 赋予临时权限

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);

2. TAG与文件路径对应关系表

NAMEVALUEPATH
TAG_ROOT_PATHroot-path/
TAG_FILES_PATHfiles-path/data/user/0/com.xxx/files
TAG_CACHE_PATHcache-path/data/user/0/com.xxx/cache
TAG_EXTERNALexternal-path/storage/emulated/0
TAG_EXTERNAL_FILESexternal-files-path/storage/emulated/0/Android/data/com.xxx/files
TAG_EXTERNAL_CACHEexternal-cache-path/storage/emulated/0/Android/data/com.xxx/cache

3. 源码浅析

3.1 配置FileProvider,做了什么

我们来看下源码

public class FileProvider extends ContentProvider {
    ...
    @GuardedBy("sCache")
    private static HashMap<String, PathStrategy> sCache = new HashMap<String, PathStrategy>();

    interface PathStrategy {
        public Uri getUriForFile(File file);

        public File getFileForUri(Uri uri);
    }

    static class SimplePathStrategy implements PathStrategy {
        private final String mAuthority;
        private final HashMap<String, File> mRoots = new HashMap<String, File>();

        public SimplePathStrategy(String authority) {
            mAuthority = authority;
        }
    }
    ...
}

我们注意到源码中有两个HashMap。第一个是static HashMap

3.1 配置FileProvider的本质是什么

我们经常用到文件Uri的场景就是APP启动安装程序界面,并传递安装文件的路径给安装程序。在Android7.0之后。程序A想要使用程序B中的文件Uri。那么程序B必须将文件路径的策略注册到FileProvider的sCache中,并且FileProvider提供了根据Uri反向解码文件路径的功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值