2013-05-19 37 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

相关问题