2012-07-22 113 views
0

我看到很多類似的問題trought stackoverflow,但沒有人幫助我。我的代碼是這樣的:Android:如何將圖像保存到緩存目錄?

String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     //create name based on time? just a simple link to photo 
     //save to cache file 
     File file = getExternalCacheDir(); 
     if (file != null) { 
      try { 
       String root = file.getAbsolutePath(); 
       File imageFile = new File(file, "m_images.jpeg"); 
       boolean tr = imageFile.mkdirs(); 
       FileOutputStream stream = new FileOutputStream(imageFile); 
       boolean complete = image.compress(Bitmap.CompressFormat.PNG, 90, stream); 
       if (!complete) { 
        Log.d("tag", "image doesn't saved"); 
       } 
       Log.d("tag", "image saved"); 
      } catch (IOException e) { 
       Log.d("tag", "Can't save image", e); 
      } 

但是當我打開FileOoutputStream我總是得到這樣的例外:

java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.whatever/cache/m_images.jpeg (Is a directory) 

你能說這是哪裏的問題的原因是什麼?爲什麼文件不會創建?

回答

2

例外/mnt/sdcard/Android/data/com.whatever/cache/m_images.jpeg (Is a directory) 表示它是一個目錄。這將是執行好當刪除線boolean tr = imageFile.mkdirs();

+0

謝謝,它有助於 – 2012-07-22 16:42:44