2010-11-19 67 views
0

我試圖將圖像保存到SD卡我卻遇到了以下錯誤:試圖保存文件,但得到「文件的父目錄不存在」,甚至試圖創建目錄後

java.io.IOException: Parent directory of file does not exist: /sdcard/skdyImages/a46e2e08-9154-4fe7-96e8-2af0a7a92867.jpg 

我有在我的權限清單

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

這裏是我的代碼:

String newName = UUID.randomUUID().toString(); 
      Bitmap bmp = ImageLoader.getInstance().getBitmap(e.getUrl()); 
      File file = new File("/sdcard/skdyImages", newName + ".jpg"); 
      file.getParentFile().mkdirs(); 
      file.createNewFile(); 
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
      bmp.compress(CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

任何想法?

回答

0

嘗試這樣的事情......

// Automatically creates folder on sdcard called /Android/data/<package>/files 
// if it doesn't exist 
File ImageDir = new File(getExternalFilesDir(null).getAbsolutePath()); 

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(ImageDir + "/" + newName + ".jpg")); 
+0

我想工作,因爲我可以看到應用程序的高速緩存大小變了,我沒有得到這個錯誤了。謝謝! – 2010-11-19 22:23:47

+0

很高興幫助。使用類似Astro的東西來檢查/ mnt/sdcard/Android /數據,你應該看到一個包含你的包名的文件夾。注意:如果您卸載了您的應用程序,該文件夾將由應用程序管理器自動刪除。您可能還想查看android.os.Environment以獲取其他get ...()方法,以返回在卸載應用程序時未刪除的公共目錄。 – Squonk 2010-11-19 23:55:01