繁体   English   中英

volley磁盘缓存的内存不足问题

[英]out of memory issue with volley's disk cache

在我的Android应用程序中,我使用Volley在自定义列表视图中加载图像。

当我刷新(删除所有项目并加载tiems)listview很多次,我的应用程序被此消息杀死

我该怎么办呢?

04-26 13:08:01.038:E / dalvikvm-heap(18040):1684947261字节分配的内存不足。 04-26 13:08:01.038:I / dalvikvm(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 04-26 13:08:01.048:D / PointerIco n(847):setHoveringSpenIconStyle1 pointerType:10001iconType:1 flag:0 04-26 13:08:01.048:D / PointerIcon(847):setHoveringSpenCustomIcon IconType相同.1 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.DiskBasedCa che.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

你有没有尝试过

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

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

这会更改缓存大小

我在将图像加载到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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM