1. 程式人生 > >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 賦予臨時許可權

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反向解碼檔案路徑的功能