首先,我想说明的是我做的Android/Java的File.mkdirs()在外部存储设备不工作
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在我的清单中指定有,我做检查Environment.MEDIA_MOUNTED。
在我看来,这真的很奇怪,它返回true,但它实际上并不创建目录。
public static void downloadFiles(ArrayList<FileList> list) {
for (FileList file: list) {
try {
// This will be the download directory
File download = new File(downloadDirPatch.getCanonicalPath(), file.getPath());
// downloadDirPatch is defined as follows in a different class:
//
// private static String updateDir = "CognitionUpdate";
// private static File sdcard = Environment.getExternalStorageDirectory();
// final public static File downloadDir = new File(sdcard, updateDir);
// final public static File downloadDirPatch = new File(downloadDir, "patch");
// final public static File downloadDirFile = new File(downloadDir, "file");
if (DEV_MODE)
Log.i(TAG, "Download file: " + download.getCanonicalPath());
// Check if the directory already exists or not
if (!download.exists())
// The directory doesn't exist, so attempt to create it
if (download.mkdirs()) {
// Directory created successfully
Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
} else {
throw new ExternalStorageSetupFailedException("Download sub-directories could not be created");
}
else {
// Directory already exists
Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} catch (ExternalStorageSetupFailedException essfe) {
essfe.printStackTrace();
}
}
}
“如果(download.mkdirs())”返回true,但是当应用程序进入实际下载文件时,它抛出一个
FileNotFoundException: open failed: ENOENT (No such file or directory)
例外,当我检查的目录之后在我的手机上,它不存在。
在程序的前面,应用程序设置父级下载目录,并且使用File.mkdir()可以正常工作,但File.mkdirs()似乎对我来说工作不正常。
是什么......我有一个类似的问题该如何解决? – Lion789