How to cache a bitmap in android?

1

Hello, I'm doing an application for my tcc, at the moment it is looking for a list of images in the database and showing in a listview in android, but when I make the list transition, it does search again, this does with the application getting too heavy, so I'd like to cache the bitmap, so when I open the app it uses the images I downloaded. I'm getting a byte array and transforming into a bitmap, this inside a baseAdapter and I'd like to know how I can cache it.

public class UsuarioAdapter extends BaseAdapter {
    private List<Usuario> usrs;
    private Context context;
    private LayoutInflater inflater;

public UsuarioAdapter(Context context, List<Usuario> usrs,
        ImageLoader mImageLoader) {
    this.usrs = usrs;
    this.context = context;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mImageLoader = mImageLoader;

}

@Override
public int getCount() {
    return usrs.size();
}

@Override
public Object getItem(int arg0) {
    return usrs.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return usrs.get(arg0).getId();
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder = null;

    if (view == null) {
        holder = new ViewHolder();
        int layout = R.layout.lista_de_usuarios;
        view = inflater.inflate(layout, null);
        view.setTag(holder);

        holder.ivFoto = (ImageView) view.findViewById(R.id.ivLvFoto);
        holder.tvNome = (TextView) view.findViewById(R.id.tvLvNome);
        holder.tvIdade = (TextView) view.findViewById(R.id.tvLvIdade);

    } else {
        holder = (ViewHolder) view.getTag();
    }
    Usuario usr = usrs.get(position);

    holder.tvNome.setText(usr.getNome());
    holder.tvIdade.setText(usr.getIdade() + " Anos");

    Bitmap bitmap = BitmapFactory.decodeByteArray(usr.getFoto(), 0,
            usr.getFoto().length);
    holder.ivFoto.setImageBitmap(bitmap);

    return view;
}

static class ViewHolder {
    ImageView ivFoto;
    TextView tvNome;
    TextView tvIdade;
}

}

The bitmap I'm referring to is in this line of code:

 Bitmap bitmap = BitmapFactory.decodeByteArray(usr.getFoto(), 0,
 usr.getFoto().length);
 holder.ivFoto.setImageBitmap(bitmap);
    
asked by anonymous 07.04.2015 / 23:45

2 answers

1

There are several ways to do this on Android. But do not reinvent the wheel. There are numerous frameworks ready to do this. Among them I recommend the

  • Volley: (developed by Google itself) link It even has a specific ImageView to cache images without any extra implementation (see NetworkImageView).
  • Picasso: One of the most used is very easy to implement and also saves a lot of work. link

You can get tutorials from any of these with a simple Google search. In order not to waste time I would use Picasso which is very simple, you simply use Gradle or Jar of it and with a single line you already get what you want. But if you want to specialize in Android dev I highly recommend studying the Volley.

    
09.04.2015 / 20:46
0

I recommend the following links, where you have examples of how to work with bitmap caching:

link link

I hope I have helped. Good luck.

    
08.04.2015 / 20:57