2014-04-26 70 views
7

在我的Android應用程序中,我使用Volley在自定義列表視圖中加載圖像。排空的磁盤緩存內存不足問題

當我刷新(刪除所有項目,並負載TIEMS)的ListView很多次, 我的應用程序被殺害,此消息

如何解決呢?

04-26 13:08:01.038:E/dalvikvm-heap(18040):1684947261字節分配的內存不足。 (18040):「Thread-11094」prio = 5 tid = 299 RUNNABLE 04-26 13:08:01.038:I/dalvikvm(18040):| group =「main」sCount = 0 dsCount = 0 obj = 0x439ea8e8 self = 0x7fb55250 04-26 13:08:01.038:I/dalvikvm(18040):| sysTid = 18946 nice = 10 sched = 0/0 cgrp = apps/bg_non_interactive handle = 2102160344 04-26 13:08:01.038:I/dalvikvm(18040):| state = R schedstat =(109248225 27367764 57)utm = 9 stm = 1 core = 2 04-26 13:08:01.038:I/dalvikvm(18040):at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache .java:〜316) 04-26 13:08:01.038:I/dalvikvm(18040):at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:526) 04-26 13:08: 01.038:I/dalvikvm(18040):at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:549) 04-26 13:08:01.038:I/dalvikvm(18040):at com.android。 volley.toolbox.DiskBasedCache $ CacheHeader.readHeader(DiskBasedCache.java:392) 04-26 13:08:01.038:I/dalvikvm(18040):at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java: 155) 04-26 13:08:01.038:I/dalvikvm(18040):at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84) 04-26 13:08:01.048:W/dalvikvm(18040):threadid = 299:線程退出與未捕獲的異常(組= 0x41745da0) 04-26 13:08:01.048:I/SpenGestureManager(847):setFocusWindow0 0426 13:08:01.048:D/PointerIcon(847):setHoveringSpenIconStyle1 pointerType:10001iconType:1標誌:0 04-26 13:08:01.048:D/PointerIcon(847):setHoveringSpenCustomIcon IconType與012相同。 04 -26 13:08:01.048:E/AndroidRuntime(18040):致命例外:Thread-11094 04-26 13:08:01.048:E/AndroidRuntime(18040):進程:com.android.myapp,PID:18040 04-26 13:08:01.048:E/AndroidRuntime(18040):java.lang.OutOfMemoryError 04-26 13:08:01.048:E/AndroidRuntime(18040):at com.android.volley.toolbox.DiskBasedCache.streamToBytes (DiskBasedCache.java:316) 04-26 13:08:01.048:E/AndroidRuntime(18040):at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:526) 04-26 13:08:01.048:E/AndroidRuntime(18040):at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:549) 04-26 13:08:01.048:E/AndroidRuntime(18040):at com.android.volley.toolbox.DiskBasedCache $ CacheHeader.readHeader( DiskBasedCache.java:392) 04-26 13:08:01.048:E/AndroidRuntime(18040):at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:155) 04-26 13:08: 01.048:E/AndroidRuntime(18040):at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84) 04-26 13:08:01.058:W/ActivityManager(847):強制整理活動com.android。 MYAPP/.feedlist。Feedlist

+0

任何解決這個? – CQM

+1

這個問題差不多2年了,但我仍在尋找解決方案。 [這個問題](https://stackoverflow.com/questions/24095909/volley-out-of-memory-error-weird-allocation-attempt/24422376#comment54546831_24422376)是相似的,但答案不適合我。 OP你有沒有想過? – yuval

回答

1

你從

https://stackoverflow.com/a/21299261/3399432

試圖

RequestQueue volleyQueue = Volley.newRequestQueue(this); 
DiskBasedCache cache = new DiskBasedCache(getCacheDir(), 16 * 1024 * 1024); 
volleyQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack())); 
volleyQueue.start(); 

這改變了高速緩存大小

1

我是有加載圖像非常相似的問題變成一個ListView。看來你必須自己處理從緩存中拉出來。

下面是我的實現,我有我的public View getView(int position, View convertView, ViewGroup parent)適配器方法:

Bitmap cachedBitmap = imageCache.getBitmap(item.getPhotoUrl()); 
if (cachedBitmap != null) 
{ 
    holder.photo.setImageBitmap(cachedBitmap); 
} 
else { 
    ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(this.context), imageCache); 
    imageLoader.get(item.getPhotoUrl(), new ImageLoader.ImageListener() { 
     @Override 
     public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { 
      if (item.getPhotoUrl() != null && response.getBitmap() != null) 
       imageCache.putBitmap(item.getPhotoUrl(), response.getBitmap()); 
     } 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      error.printStackTrace(); 
     } 
    }); 

    //This is a custom imageview, you can create your own implementation for loading the image url to the imageview 
    holder.photo.setImageUrl(item.getPhotoUrl(), imageLoader); 
}