版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/future234/article/details/81214244
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与文件路径对应关系表
NAME | VALUE | PATH |
---|---|---|
TAG_ROOT_PATH | root-path | / |
TAG_FILES_PATH | files-path | /data/user/0/com.xxx/files |
TAG_CACHE_PATH | cache-path | /data/user/0/com.xxx/cache |
TAG_EXTERNAL | external-path | /storage/emulated/0 |
TAG_EXTERNAL_FILES | external-files-path | /storage/emulated/0/Android/data/com.xxx/files |
TAG_EXTERNAL_CACHE | external-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反向解码文件路径的功能