FileProvider使用以及源码浅析

简介: 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 赋予临时权限2. TAG与文件路径对应关系表

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);

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


image.png

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反向解码文件路径的功能


相关文章
|
Java Android开发 索引
Android 解析DEX文件
1. DEX文件简介 1). 基本格式 Android DEX文件格式--非虫大神杰作.png dex-file-general-structure.
1218 0
|
2月前
|
Shell Android开发
安卓scheme_url调端:在AndroidManifest.xml 中如何配置 Intent-filter?
为了使Android应用响应vivo和oppo浏览器的Deep Link或自定义scheme调用,需在`AndroidManifest.xml`中配置`intent-filter`。定义启动的Activity及其支持的scheme和host,并确保Activity可由外部应用启动。示例展示了如何配置HTTP/HTTPS及自定义scheme,以及如何通过浏览器和adb命令进行测试,确保配置正确无误。
|
5月前
|
编译器 Android开发
Android S内置APK时AndroidManifest使用uses-library编译报错
Android S内置APK时AndroidManifest使用uses-library编译报错
159 0
|
XML 缓存 API
Android 7.0之访问文件的权限和FileProvider类
转载请标明出处: http://blog.csdn.net/djy1992/article/details/72533310 本文出自:【奥特曼超人的博客】 权限更改 Android 7.0 做了一些权限更改,这些更改可能会影响您的应用。
3680 0
|
存储 Android开发
【错误记录】Android 文件分享 FileProvider 设置错误
【错误记录】Android 文件分享 FileProvider 设置错误
201 0
【错误记录】Android 文件分享 FileProvider 设置错误
|
安全 Java Android开发
【Android 安全】DEX 加密 ( Application 替换 | 分析 Activity 组件中获取的 Application | ActivityThread | LoadedApk )(三)
【Android 安全】DEX 加密 ( Application 替换 | 分析 Activity 组件中获取的 Application | ActivityThread | LoadedApk )(三)
127 0
|
安全 Java Android开发
【Android 安全】DEX 加密 ( Application 替换 | 分析 Activity 组件中获取的 Application | ActivityThread | LoadedApk )(一)
【Android 安全】DEX 加密 ( Application 替换 | 分析 Activity 组件中获取的 Application | ActivityThread | LoadedApk )(一)
173 0
|
安全 Java Android开发
【Android 安全】DEX 加密 ( Application 替换 | 分析 Activity 组件中获取的 Application | ActivityThread | LoadedApk )(二)
【Android 安全】DEX 加密 ( Application 替换 | 分析 Activity 组件中获取的 Application | ActivityThread | LoadedApk )(二)
114 0
|
安全 数据安全/隐私保护 Android开发
【Android 安全】DEX 加密 ( Application 替换 | ActivityThread 中的 mAllApplications 集合添加 Application )
【Android 安全】DEX 加密 ( Application 替换 | ActivityThread 中的 mAllApplications 集合添加 Application )
138 0
|
安全 Java Android开发
【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | LoadedApk 源码分析 )
【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | LoadedApk 源码分析 )
369 0