首先,我想说明我确实有
<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();
}
}
}
"if (download.mkdirs())" 返回 true,但是当应用程序实际下载文件时,它会抛出一个
FileNotFoundException: open failed: ENOENT (No such file or directory)
异常,然后当我在手机上检查目录时,它不存在。
在程序的早期,应用程序设置了父下载目录,并且使用 File.mkdir() 一切正常,但 File.mkdirs() 对我来说似乎无法正常工作。