2013-05-19 34 views
1

首先,我想說明的是我做的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()似乎對我來說工作不正常。

+0

是什麼......我有一個類似的問題該如何解決? – Lion789

回答

2

您的問題沒有給出關於FileNotFoundException的更多細節。檢查觸發這個的路徑。忘記你的想法認爲的路徑是,記錄或通過調試器運行它,看看它是什麼。

根據沒有正確創建的目錄,(用你的眼睛)驗證路徑確實是認爲是。我看到你已經在記錄download.getCanonicalPath,請檢查你的日誌是什麼。

最後,是Download.download真的可以節省你的東西認爲它呢?在你給它打電話之前,你正在準備和驗證一個使用download的目錄,但是當你撥打Download.download時你沒有使用download,所以不可能說明。

順便說一句,不要重複自己,你可以重寫,而無需重複Download.download行:

 if (!download.exists()) 
      if (!download.mkdirs()) { 
       throw new ExternalStorageSetupFailedException("Download sub-directories could not be created"); 
      } 
     } 
     Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true); 
+0

路徑是正確的,我已經檢查過了。 Download.download真的把它保存在我認爲的地方。路徑是「/ storage/emulated/0/CognitionUpdate/patch/path/of/this」,Download.download會嘗試將其保存到「/ storage/emulated/0/CognitionUpdate/patch/path/of/this/this。補丁」。感謝提示,我也一定會這樣做。 – DemonWav

+0

此外,全部例外是: 'W/System.err:java.io.FileNotFoundException:/storage/emulated/0/CognitionUpdate/patch/path/of/this/this.patch:打開失敗:ENOENT(否這樣的文件或目錄)' – DemonWav

+0

嗯......在.mkdirs()成功後,'.exists()'和'.isDirectory()'也返回true嗎? – janos

相關問題