out of memory issue with volley's disk cache

后端 未结 2 987
青春惊慌失措
青春惊慌失措 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);
    }
    

提交回复
热议问题