out of memory issue with volley's disk cache

后端 未结 2 988
青春惊慌失措
青春惊慌失措 2021-02-06 08:52

In my app for Android I\'m using Volley for loading images in custom listview.

when i refresh(delete all items and load tiems) listview many times, my app is killed with

相关标签:
2条回答
  • 2021-02-06 09:42

    I was having a very similar issue with loading images into a ListView. It seems you have to handle pulling from the cache yourself.

    Below is my implementation which I have in my public View getView(int position, View convertView, ViewGroup parent) adapter method:

    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);
    }
    
    0 讨论(0)
  • 2021-02-06 09:43

    Have you tried

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

    from

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

    This changes the cache size

    0 讨论(0)
提交回复
热议问题