FileProvider和辅助外部存储 [英] FileProvider and secondary external storage

查看:251
本文介绍了FileProvider和辅助外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 FileProvider 从SECONDARY外部存储中提供文件?

How can I serve files from the SECONDARY external storage using the FileProvider?

FileProvider 的当前实现仅处理 ContextCompat.getExternalFilesDirs

...    
} else if (TAG_EXTERNAL_FILES.equals(tag)) {
   File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
   if (externalFilesDirs.length > 0) {
       target = externalFilesDirs[0];
   }
}
...

似乎没有办法为 FileProvider 定义与辅助外部存储路径匹配的< path> 条目...

It seems, that there is no way to define a <path> entry for the FileProvider, that matches the secondary external storage path...

推荐答案

由于以下代码,FileProvider不支持辅助存储:

FileProvider not support secondary storage because of the code below:

support:support-core-utils:26.1.0 FileProvider中的代码

Code from support:support-core-utils:26.1.0 FileProvider

            } else if (TAG_EXTERNAL_FILES.equals(tag)) {
                File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
                if (externalFilesDirs.length > 0) {
                    target = externalFilesDirs[0];// Code here, That's why!!!
                }
            } else if (TAG_EXTERNAL_CACHE.equals(tag)) {

但是,FileProvider中有一个特殊的TAG: root-path ,在官方参考中没有涉及.

However, there is a special TAG in FileProvider : root-path which is not covered in official reference.

            if (TAG_ROOT_PATH.equals(tag)) {
                target = DEVICE_ROOT;// DEVICE_ROOT = new File("/");
            } else if (TAG_FILES_PATH.equals(tag)) {

因此,root-path匹配所有路径.

So, root-path matches all the path.

只需在您的FileProvider xml中键入此代码,然后FileProvider即可处理辅助存储中的文件.

Just type this code in your FileProvider xml, then FileProvider can handle File in secondary storage.

<root-path name="root" path="." />

请注意,这可能会泄漏您的目录结构.

Be aware, it may leak your directory structure.

这篇关于FileProvider和辅助外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆