1

I have an app in which i can add many products each with an image. I am storing all images to a fixed path in my storage. My issue is that when i load all products into list view it takes too much of time to decode all images from the path. I am using custom adapter.

My adapter

public class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return planList.size();
    }
    @Override
    public Object getItem(int position) {
        return planList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ProductsModel db_data = planList.get(position);

        if (convertView == null) {
            convertView = View.inflate(getApplicationContext(), R.layout.product_list, null);
        }
        ImageView btn1 = (ImageView) convertView.findViewById(R.id.imageView);
        TextView name = (TextView) convertView.findViewById(R.id.name);
        TextView stock = (TextView) convertView.findViewById(R.id.stock);
        TextView amount = (TextView) convertView.findViewById(R.id.amount);

        name.setText(db_data.getName());
        stock.setText(db_data.getStock());
        amount.setText(db_data.getSprice());

        try{
            Bitmap bm = BitmapFactory.decodeFile(db_data.getImage());
            if (bm != null) {
                btn1.setImageBitmap(bm);
            } else {
                btn1.setImageResource(R.drawable.photo);
            }
        }catch (Exception e) {

            btn1.setImageResource(R.drawable.photo);
        }
        return convertView;
    }
}

When i click a button to show all items in listview it takes too much time for starting the list view activity. i found that "BitmapFactory.decodeFile(db_data.getImage());" is more time consuming. How can i populate ImageView in ListView item from image path,more efficiently.

Visakh
  • 259
  • 3
  • 15

1 Answers1

3

You can use Glide or Picasso for image loading

https://github.com/bumptech/glide

https://github.com/square/picasso/tree/master/picasso