【发布时间】:2016-02-11 15:56:14
【问题描述】:
所以我知道有 SD 卡访问 API 允许我们通过 DocumentProvider 和 DocumentFiles 写入文件。我已经让它在可移动的 SD 卡上工作。我总是对外部和内部存储感到困惑。我一直认为外部存储始终是 SD 卡,但今天我知道并非如此。
所以我有三个问题。
问题1,如何知道文件是存储在外部模拟存储还是存储卡中? 一种解决方案可能是在文件路径中搜索“sdcard0”或“emulated”的实例。 这个解决方案会一直有效吗?我的意思是在所有手机上?
问题 2 在模拟存储(不可移动外部存储)普通文件或 DocumentFile 上写入文件时使用什么?
问题 3 如果 Q2 的解决方案是 Document File 那么为什么这不起作用?
私有静态字符串[] 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()]);
}
【问题讨论】:
标签: android android-5.0-lollipop