无法写入Android SD卡中特定于应用程序的目录
我从KitKat中学到,应用程序只能写入其特定目录 但奇怪的是,我也无法写入我的特定应用程序目录 获取sd卡目录的代码无法写入Android SD卡中特定于应用程序的目录,android,android-sdcard,Android,Android Sdcard,我从KitKat中学到,应用程序只能写入其特定目录 但奇怪的是,我也无法写入我的特定应用程序目录 获取sd卡目录的代码 Process process = new ProcessBuilder().command("mount").start(); process.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Process process = new ProcessBuilder().command("mount").start();
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
// Output the line of output from the mount command
logger.debug(" {}", line);
if (line.startsWith("/dev/block/vold/")) {
String[] tokens = line.split(" ");
if (tokens.length >= 3 && (tokens[2].equals("vfat") || tokens[2].equals("exfat"))) {
String path = tokens[1];
File file = new File(path);
if (file.exists() && file.isDirectory()) {
logger.debug("Detected SD card at {}", file.getPath());
if (!file.canWrite()) {
logger.warn("The SD card path {} is reporting that it is not writable", file.getPath());
}
// path = basecontext.getExternalFilesDir(null).getPath();
return path;
}
}
}
}
获取文件的代码
以下是我如何构造文件路径:
sdCardDirectory是我得到的如下目录:/storage/extSdCard/
目录和子目录是我的应用程序sepcifc子目录,但显然位于应用程序中特定于应用程序的目录中
sdCardDirectory + File.separator + "Android" + File.separator + "data" + File.separator
+ <my app package> + File.separator + subdirectory
+ File.separator + directory+ File.separator + document.getRepositoryId() + FILENAME_SEPARATOR
+ fetchObjectId(document);
我越来越
java.io.FileNotFoundException: /storage/extSdCard/Android/data/myapp/cache/downloaded/OhCQL_RQl8IJcVlO5T1MX4-3SQg_mMDT5PWtf-IYmE0: open failed: EROFS (Read-only file system)
其中myapp是我的应用程序包名称
更新这是我打开的Android bug的链接
干杯,
索拉夫
但奇怪的是,我也无法写入我的特定应用程序目录
这些代码不一定会给你任何你可以使用的东西。请使用getExternalFilesDirs注意复数形式;返回列表中的第二个和后续条目将来自可移动存储(如果可用)
您可能希望阅读更多背景信息。请发布一些包含正确logcat错误的代码。您是否在清单中添加了写入外部存储和读取外部存储的权限?写入外部存储已添加,我认为读取外部存储已隐式添加。您没有告诉返回的路径是什么。此外,您不会显示如何启动变量文件。这样,我们就无法检查文件未找到异常中提到的路径。@greenapps返回的路径是/storage/extSdCard/。因此路径正确返回响应的hanks标记。但是这个方法来自API级别19。我也得到了API级别17中的只读错误。@saurav:但是这个方法来自API级别19-正确。有ContextCompat及其方法的版本,尽管它仍然只返回API级别19+设备上可移动媒体上的路径。它只允许您自己避免Build.VERSION.SDK_INT检查。我还收到API级别17中的只读错误-您现有的代码在操作系统版本或设备中不可靠。在API级别19之前,可移动媒体不是Android SDK的一部分。这在我的回答中链接到的博客文章中有所涉及。再次感谢Mark的回复。GetExternalFileDirs返回API级别17中的内部模拟存储/存储/模拟/0/Android/data/myapp/文件。API级别17是否对SD卡也有相同的写入限制?@saurav:API级别19之前的可移动存储行为取决于设备制造商。在我的4.4.2设备上,GetExternalFileDirs只有一项。与saurav提到的相同。它没有提到可移动存储。此外,应用程序无法写入micro SD卡。甚至连他们自己的Android/data/package/files目录都没有,因为该目录不存在,无法创建。有趣的是,应用程序可以在内部/SD卡的任何地方写入。
java.io.FileNotFoundException: /storage/extSdCard/Android/data/myapp/cache/downloaded/OhCQL_RQl8IJcVlO5T1MX4-3SQg_mMDT5PWtf-IYmE0: open failed: EROFS (Read-only file system)