首先,我想說明的是我做的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