0

I have a ViewPager with 4 ListViews.The list views all download unique Images and I want to cache them but there is an error and the app works very slow. Is there a way to cache all images in 1 LruCache or something like that? This is my code so far first list Adapter

public class TwitchAdapter extends ArrayAdapter<Twitch>{
private Context context;
private int resourceId;
private ArrayList<Twitch> twitchList;
LruCache<Integer,Bitmap> logoCache;
LruCache<Integer,Bitmap> iconCache;
public TwitchAdapter(Context c, int resId, ArrayList<Twitch> objects){
    super(c, resId, objects);
    this.context = c;
    this.resourceId = resId;
    this.twitchList = objects;
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    iconCache = new LruCache<Integer, Bitmap>( maxMemory / 16);
    logoCache = new LruCache<Integer, Bitmap>( maxMemory / 64);
}
public View getView(final int position, View convertView, final ViewGroup parent){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(resourceId, parent, false);
    final Twitch twitch = twitchList.get(position);

    TextView viewrs = (TextView) view.findViewById(R.id.viewers);
    TextView language = (TextView) view.findViewById(R.id.language);
    TextView status = (TextView) view.findViewById(R.id.streamer_status);
    TextView name = (TextView) view.findViewById(R.id.streamer_name);
    ImageView profile = (ImageView) view.findViewById(R.id.logo);
    CircleImageView imageView = (CircleImageView) view.findViewById(R.id.twitch_icon);
    int id = twitch.getId();
    status.setText(twitch.getStatus());
    language.setText(twitch.getLanguage());
    name.setText(twitch.getName());
    viewrs.setText(twitch.getViewrs()+"");
    Bitmap bitmapIcon = iconCache.get(id+1);
    if (bitmapIcon != null){


        imageView.setImageBitmap(bitmapIcon);
    }else {
        LoadImageTask(twitch,imageView);
    }
    Bitmap bitmapLogo = logoCache.get(twitch.getId());
    if (bitmapLogo != null){
        profile.setImageBitmap(bitmapLogo);
    }else {
        LoadIconTast(twitch,profile);
    }

    return view;
}



public void LoadImageTask(final Twitch twitch, final ImageView iconView){

        ImageRequest request  =new ImageRequest(twitch.getIconUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
           iconView.setImageBitmap(response);
            iconCache.put(twitch.getId()+1,response);
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);

}
private void LoadIconTast(final Twitch twitch, final ImageView profileView) {
    ImageRequest request  =new ImageRequest(twitch.getLogoUrl(), new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            profileView.setImageBitmap(response);
            iconCache.put(twitch.getId(),response);
            notifyDataSetChanged();
        }
    }, 0, 0, null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    RequestQueue quew = Volley.newRequestQueue(context);
    quew.add(request);
}

}

And another one exactly like this and this is error

12-06 19:15:24.392 31970-2219/app.mma.introsliderproject E/AndroidRuntime: FATAL EXCEPTION: Thread-100374
                                                                       Process: app.mma.introsliderproject, PID: 31970
                                                                       java.lang.OutOfMemoryError: Failed to allocate a 17 byte allocation with 27048 free bytes and 26KB until OOM
                                                                           at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:323)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:541)
                                                                           at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:564)
                                                                           at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:404)
                                                                           at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:157)
                                                                           at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)

2 Answers2

0

You can use image caching library like Glide or Picasso. They can automatically resize your images to fit view size and consume less memory.

Pavel Kuznetsov
  • 344
  • 2
  • 8
0

i wrote this and its working

public class Cache  {
LruCache<Integer,Bitmap> logoCache = new LruCache<>((int) (Runtime.getRuntime().maxMemory()/1024/8));

public void setCache(int key, Bitmap bitmap){

    logoCache.put(key,bitmap);
}
public Bitmap getCache(int key){

    Bitmap bitmap = logoCache.get(key);
    return bitmap;
}