2011-09-12 107 views
1

在我的應用程序中,我從網上下載圖像。爲此,我第一次從網上下載圖像,這些圖像存儲在SD卡中。下次,我正在檢查這些圖像是否在SD卡中。如果是的話,從SD卡獲取,否則我從網上下載。這些圖像顯示爲列表。我反覆(意味着不斷上移/下移)滾動列表,然後我的應用程序崩潰,我得到了內存不足的例外。如何處理它。有誰能夠幫助我。android-內存不足問題

堆棧跟蹤:

09-12 13:40:42.640: ERROR/AndroidRuntime(3426): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7879KB, Allocated=3362KB, Bitmap Size=11402KB) 
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):  at android.graphics.BitmapFactory.nativeDecodeFile(Native Method) 
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:355) 
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:433) 
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):  at com.ibkr.elgifto.GiftCategories$itemlistadapter$4.getDrawableFromUrl(GiftCategories.java:830) 
09-12 13:40:42.640: ERROR/AndroidRuntime(3426):  at com.ibkr.elgifto.GiftCategories$itemlistadapter$4.run(GiftCategories.java:739) 
09-12 13:53:32.450: INFO/WSP(332): [Receiver] next auto-sync alarm event is 180 mins later, at time: Mon Sep 12 16:53:32 GMT+05:30 2011 

代碼

private Drawable getDrawableFromUrl(String imageUrl) { 

    // TODO Auto-generated method stub 
    String filename = imageUrl; 
    filename = filename.replace("/", "+"); 
    filename = filename.replace(":", "+"); 
    filename = filename.replace("~", "s"); 
    File elgiftoimagesref = new File("/sdcard/Elgiftoimages/"); 

    elgiftoimagesref.mkdir(); 

    final File file = new File(elgiftoimagesref, filename); 
    BitmapDrawable SDdrawable = null; 

    boolean exists = file.exists(); 
    if (!exists) { 
     try { 
      URL myFileUrl = new URL(imageUrl); 
      HttpURLConnection conn = (HttpURLConnection) myFileUrl 
        .openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      final Bitmap result = BitmapFactory.decodeStream(is); 
      is.close(); 
      new Thread() { 
       public void run() { 
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
        if (result != null) { 
         result.compress(Bitmap.CompressFormat.JPEG, 40, 
           bytes); 
        } 
        try { 

         FileOutputStream fo; 
         fo = new FileOutputStream(file); 
         fo.write(bytes.toByteArray()); 

         fo.flush(); 
         fo.close(); 
         // result.recycle(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      }.start(); 
      BitmapDrawable returnResult = new BitmapDrawable(result); 
      return returnResult; 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } else { 

     // Here i am getting the out of memory error. 
     SDdrawable = new BitmapDrawable(BitmapFactory.decodeFile(file 
       .toString())); 
     return SDdrawable; 
    } 
}  
+0

降低圖像的大小,並嘗試 – Abhi

+0

在這裏,我的情況下我壓縮圖像是不夠的,那麼如何減小尺寸? – naresh

+0

你似乎將它們解壓縮到內存中,然後在保存中重新壓縮它們......也許跳過並按原樣保存流。 – Torp

回答

0

我想你不使用ViewHolder模式的列表視圖。在這種情況下,你應該得到這樣的錯誤,因爲所有的列表項都可以在內存中看到,不管它是否可見。通過使用ViewHolder,內存只被分配給那些可見和當前在屏幕上的項目。 ViewHolder patternincreses 150%的性能 有關ViewHolder視頻教程退房this。如果你已經實現,那麼它是內存泄漏的可以參考this