2012-02-16 216 views
1

因此,我的最新應用運行到這個問題,它在logcat中抱怨說,設備上沒有剩餘空間來保存文件,但這絕對不是這種情況因爲我可以關閉我的應用程序,打開相機並拍照。其他人如何處理這個問題。Android - 如何解決「設備上沒有剩餘空間」錯誤

編輯:此方法

private void writeFile(Bitmap bmp, File f) { 
    FileOutputStream out = null; 

    try { 
     out = new FileOutputStream(f); 
     bmp.compress(Bitmap.CompressFormat.PNG, 80, out);//<---error here 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 
     Log.w("nullpointerException on image error", "nullpointer"); 
    } catch (FileNotFoundException e) { 
     Log.w("fileNotfoundException on image error", "filenotfound"); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally { 
     try { if (out != null) out.close(); } 
     catch(Exception ex) {} 
    } 
} 

出現的錯誤,這是對這個錯誤的logcat的報告:

java.io.IOException: no space left on device 
at org.apache.harmony.luni.platform.OSFileSystem.write(Native Method) 
at dalvik.system.BlockGuard$WrappedFileSystem.write(BlockGuard.java:171) 
at java.io.FileOutputStream.write(FileOutputStream.java:300) 
at android.graphics.Bitmap.nativeCompress(Native Method) 
at data.ImageManager.writeFile(ImageManager.java:215) 
at data.ImageManager.getBitmap(ImageManager.java:192) 
at data.ImageManager.access$1(ImageManger.java:102) 
at data.ImageManager$ImageQueueManager.run(ImageManager.java:290) 
at java.lang.Thread.run(Thread.java:1019) 

編輯:繼承人如何我雖然創建於外部存儲器

目錄

這就是我用來創造我認爲是SD卡的地方

String sdState = android.os.Environment.getExternalStorageState(); 
    if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     File sdDir = android.os.Environment.getExternalStorageDirectory();  
     cacheDir = new File(sdDir,"data/gr"); 
    } 
    else 
     cacheDir = context.getExternalCacheDir(); 

    if(!cacheDir.exists()) 
     cacheDir.mkdirs(); 


    if(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){ 
     File adSdDir =  android.os.Environment.getExternalStorageDirectory(); 
     adCacheDir = new File(adSdDir,"data/ad"); 
    }else 
     adCacheDir = context.getExternalCacheDir(); 

    if(!adCacheDir.exists()) 
     adCacheDir.mkdirs(); 
} 

,然後在我尋找或創造的形象進入該目錄的方法:

private Bitmap getBitmap(ImgRequestObj ids) { 
    String url = null; 
    String filename = ids.objId.toString(); 
    File f = null; 

    try{ 
     if(ids.objName.equals("mark")){ 
      url = graffMarksURL; 
      f = new File(cacheDir, filename); 
     }else if(ids.objName.equals("admark")){ 
      url = adMarksURL; 
      f = new File(adCacheDir, filename); 
     } 



    // Is the bitmap in our cache? 
    Bitmap bitmap = BitmapFactory.decodeFile(f.getPath()); 
    if(bitmap != null) return bitmap; 

    // Nope, have to download it 
    try { 
      BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 

      bfOptions.inDither=false;      //Disable Dithering mode 

      bfOptions.inPurgeable=true;     //Tell to gc that whether it needs free memory, the Bitmap can be cleared 

      bfOptions.inInputShareable=true;    //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 


      DefaultHttpClient client; 
      HttpResponse response = null; 
      HttpParams params = null;// new Http parameters to be used for extra buffering room in VM 
      HttpPost post = null; 
      InputStream is = null; 
      BufferedInputStream bis = null; 
      Bitmap bmp = null; 
      ArrayList<NameValuePair> nvp1 = new ArrayList<NameValuePair>(); 

      nvp1.add(new BasicNameValuePair("id", ids.objId.toString())); 
      // - martin passing id to php script select statement 
      client = new DefaultHttpClient(); 

      try { 

       post = new HttpPost(url); 
       params = post.getParams();//Setting the new Http params to post 
       post.setEntity(new UrlEncodedFormEntity(nvp1)); 
       try{ 
       HttpConnectionParams.setSocketBufferSize(params, 8192);//Defining the new buffer size 
       response = client.execute(post); 

       } 
       catch(HttpResponseException e){ 
        e.getCause(); 
            } 
      //Capture response from query and prepare as input 
      HttpEntity entity = response.getEntity(); 
      // move content to Input Stream 
       is = entity.getContent(); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

       bis = new BufferedInputStream(is); 
       try{ 
       bmp = BitmapFactory.decodeStream(bis, null, bfOptions); 
       }finally{} 

        if(is != null) 
         try { 
          is.close(); 
          //response = null; 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        //Log.w("inputstream of image closed", "for this image "+id.toString()); 
        if(bis != null) 
         try { 
          //response = null; 
          bis.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        bitmap = bmp; 
        writeFile(bitmap, f); 
        try{ 
         bmp = null; 
        }catch(NullPointerException e){ 
         e.printStackTrace(); 
        } 
        return bitmap;} 
       catch(Exception e){ 
      e.printStackTrace(); 
      } 
     return bitmap; 
    }catch(NullPointerException e){ 
     e.printStackTrace(); 
    } 
    return null; 
     } 
+0

和你如何準備'文件f'? – waqaslam 2012-02-16 23:06:52

+0

你如何構建文件f的路徑?如果你不小心,你可能會使用內部存儲器,而不會影響平臺相機。 – 2012-02-16 23:09:22

+0

即時通訊使用上面添加的代碼。 – 2012-02-16 23:16:53

回答

1

你實際上是使用內部存儲器通過在其他語句中使用getCacheDir()。但是,如果你想存儲大量數據,那麼它建議使用getExternalCacheDir(),因爲並非所有的Android手機都有巨大的內部存儲空間,但他們確實可能擁有更大的外部存儲空間。

if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { 
    File sdDir = context.getExternalCacheDir(); 
    cacheDir = new File(sdDir,"data"); 
} 
else 
    Log.e("storage", "SD card not found"); 
+0

這沒有工作:(我擦除了我的應用程序的所有數據,並卸載它,然後使用你建議我嘗試運行它,但它仍然結束了在內部存儲器上創建文件,並且一旦圖像達到60我不能再拍一張照片 – 2012-02-16 23:33:54

+0

這次你是否使用'getExternalCacheDir()' – waqaslam 2012-02-16 23:35:11

+0

是的更新以顯示我現在使用的是什麼 – 2012-02-16 23:36:09

0

我最終改變我的代碼,我創建我的目錄下面:

String sdState = android.os.Environment.getExternalStorageState(); 
    if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) { 
     File sdDir = android.os.Environment.getExternalStorageDirectory();  
     cacheDir = new File(sdDir,"data/gr"); 
    } 
    else 
     cacheDir = context.getExternalFilesDir(sdState); <--changed 

    if(!cacheDir.exists()) 
     cacheDir.mkdirs(); 


    if(sdState.equals(android.os.Environment.MEDIA_MOUNTED)){ 
     File adSdDir = android.os.Environment.getExternalStorageDirectory(); 
     adCacheDir = new File(adSdDir,"data/ad"); 
    }else 
     adCacheDir = context.getExternalFilesDir(sdState); <--changed 

    if(!adCacheDir.exists()) 
     adCacheDir.mkdirs(); 
} 

到目前爲止利用這個我能夠再次拯救我使用相機拍攝的圖像在全部照片在我的地區下載。

相關問題