FileProvider之file_path

2019-12-17  本文已影响0人  钦_79f7
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <paths>
        <external-path
            name="external_storage_root"
            path="">
            <!--Environment.getExternalStorageDirectory()-->
            <!--/storage/emulated/0-->
            <!--存储的根目录-->
        </external-path>

        <files-path>
            <!--context.getFilesDir()-->
            <!--内部存储的files目录-->
            <!--/data/user/0/com.stone.testdemo/files-->
        </files-path>

        <cache-path>
            <!--context.getCacheDir()-->
            <!--内部存储的cache目录-->
            <!--/data/user/0/com.stone.testdemo/cache-->
        </cache-path>

        <external-files-path>
            <!--ContextCompat.getExternalFilesDirs-->
            <!--外部私有存储的files目录-->
            <!--/storage/emulated/0/Android/data/com.stone.testdemo/files-->
        </external-files-path>

        <external-cache-path>
            <!--ContextCompat.getExternalCacheDirs-->
            <!--外部私有存储的cache目录-->
            <!--/storage/emulated/0/Android/data/com.stone.testdemo/cache-->
        </external-cache-path>

        <external-media-path>
            <!--context.getExternalMediaDir-->
            <!--/storage/emulated/0/Android/media/com.stone.testdemo-->
        </external-media-path>
    </paths>
</resources>

源码

FileProvider#parsePathStrategy中关于共享路劲的解析逻辑部分

File target = null;
if ("root-path".equals(tag)) {
    target = DEVICE_ROOT;
} else if ("files-path".equals(tag)) {
    target = context.getFilesDir();
} else if ("cache-path".equals(tag)) {
    target = context.getCacheDir();
} else if ("external-path".equals(tag)) {
    target = Environment.getExternalStorageDirectory();
} else {
    File[] externalMediaDirs;
    if ("external-files-path".equals(tag)) {
        externalMediaDirs = ContextCompat.getExternalFilesDirs(context, (String)null);
        if (externalMediaDirs.length > 0) {
            target = externalMediaDirs[0];
        }
    } else if ("external-cache-path".equals(tag)) {
        externalMediaDirs = ContextCompat.getExternalCacheDirs(context);
        if (externalMediaDirs.length > 0) {
            target = externalMediaDirs[0];
        }
    } else if (VERSION.SDK_INT >= 21 && "external-media-path".equals(tag)) {
        externalMediaDirs = context.getExternalMediaDirs();
        if (externalMediaDirs.length > 0) {
            target = externalMediaDirs[0];
        }
    }                    
}
上一篇下一篇

猜你喜欢

热点阅读