2012-02-11 43 views
0

我正在嘗試將JPG圖像寫入外部SD卡。但是,我得到System.err FileNotFoundException: /mnt/sdcard/test.images/temp/savedImage (no such file or directory)。創建該目錄似乎也失敗了,並在LogCat中給出了false,而且在我的SD卡上查看時也無法看到該文件夾​​。嘗試寫入外部SD的Android錯誤

我的代碼如下:

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
     File folder = new File(Environment.getExternalStorageDirectory() + "/test.images/temp"); 
     try { 
      if(!folder.exists()){ 
       boolean dir = new File(Environment.getExternalStorageDirectory() + "/test.images/temp").mkdir(); 
       Log.v("creating directory", Boolean.toString(dir)); 
      } 
      File imageOutputFile = new File(Environment.getExternalStorageDirectory() + "/test.images/temp", "savedImage"); 
      FileOutputStream fos = new FileOutputStream(imageOutputFile); 
      Image.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
} 

我有權<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 在清單中,並已清理和重建。

+0

'文件imageOutputFile =新的文件(Environment.getExternalStorageDirectory()+ 「/test.images/temp/」, 「savedImage.jpg」);' – 2012-02-11 01:37:41

+2

嘗試的mkdir * S *() – 2012-02-11 01:38:41

+0

作品,感謝圭多。 – 2012-02-11 01:41:47

回答

1

使用mkdirs()而不是mkdir()。

Guido在評論中發佈了適用於我的解決方案。我會重複一遍,以確保它可以成爲答案。