繁体   English   中英

在棒棒糖中写入文件到模拟存储中?

[英]Writing files in lollipop in emulated storage?

因此,我知道有SD卡访问API,该API允许我们通过DocumentProvider和DocumentFiles写入文件。 我已经使其在可移动SD卡上工作。 我总是对外部和内部存储感到困惑。 我一直以为外部存储始终是SD卡,但今天我知道事实并非如此。

所以我有三个问题。

问题1 ,如何知道文件是否存储在外部模拟存储或sdcard中? 一种解决方案可能是通过在文件路径中搜索“ sdcard0”或“仿真”实例。 此解决方案将始终有效吗? 我是说在所有电话上?

问题2用于在模拟存储(不可移动外部存储)的普通文件或DocumentFile上写入文件的方法是什么?

问题3如果Q2的解决方案是Document File,那么为什么不起作用?

私有静态String [] getExtSdCardPaths(){列表路径= new ArrayList <>();

for (File file : GlobalSongList.GetInstance().getApplicationContext().getExternalFilesDirs("external")) {

    if (file != null && !file.equals(GlobalSongList.GetInstance().getApplicationContext().getExternalFilesDir("external"))) {
        int index = file.getAbsolutePath().lastIndexOf("/Android/data");
        if (index < 0) {
            Log.w("StorageAccessAPI", "Unexpected external file dir: " + file.getAbsolutePath());
        }
        else {
            String path = file.getAbsolutePath().substring(0, index);
            try {
                path = new File(path).getCanonicalPath();
            }
            catch (IOException e) {
                // Keep non-canonical path.
            }
            paths.add(path);
        }
    }
}
return paths.toArray(new String[paths.size()]);

}

因此,我知道有SD卡访问API,该API允许我们通过DocumentProvider和DocumentFiles写入文件。

您指的是Storage Access Framework 这样,您就可以在文档提供者支持流的情况下读写 ,而不是文件。 用户选择要使用的文档提供者,而后者又确定流内容的存储位置。 那可以是本地的(例如, 外部存储可移动存储 )或远程的(Google驱动器,Dropbox,Samba文件服务器,Web服务器,FTP服务器,SFTP服务器等)。

如何知道文件是否存储在外部仿真存储或sdcard中?

如果使用的是Storage Access Framework,则不知道流内容的存储位置。

在模拟存储(不可移动外部存储)普通文件或DocumentFile上写入文件时使用什么?

如果您明确要使用外部存储,请使用外部存储。

使用存储访问框架, 用户可以选择流内容的存储位置,该内容可以是外部存储,也可以不是外部存储。

为什么不起作用?

我不知道你期望怎么做。 我希望它返回一系列无用的字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM