0

i want load image on listview,i can download image from a url and show on listview but speed is very slowly. how can i improve load speed? please help me. i use of this code for download image from url:

ImageService Class:

 InputStream input = null;
     Bitmap bitmap = null;
     try {
         URL url = new URL(src);
         // We open the connection
         URLConnection conection = url.openConnection();

         conection.connect();
         input = new BufferedInputStream(url.openStream(), 8192);
         // we convert the inputStream into bitmap
       bitmap = BitmapFactory.decodeStream(input);
       input.close();
       Log.e("ERROR:","OOOOOOOOOK");
     } catch (Exception e) {
          Log.e("eRROR:", e.getMessage());

       }
       return bitmap;

}

ProductAdapter Class for custome listview:

    private LayoutInflater mInflater;

private List items = new ArrayList();

public ProductAdapter(Context context, List items) {
    mInflater = LayoutInflater.from(context);
    this.items = items;
}

public int getCount() {
    return items.size();
}

public Product getItem(int position) {
    return (Product) items.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    Product s = (Product) items.get(position);
    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.row_layout, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.pic = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.name.setText(s.getName());
    if (s.getImage() != null) {
        holder.pic.setImageBitmap(s.getImage());
    } else {

        holder.pic.setImageResource(R.drawable.ic_launcher);
    }
    return convertView;
}

static class ViewHolder {
    TextView name;

    ImageView pic;
}
Nima.S-H
  • 777
  • 1
  • 9
  • 19
  • You can see from this post, Really nice http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – CodeWarrior Jun 09 '14 at 07:26

6 Answers6

1

use these libraries , its very good and simple :D

This libraries :

picasso

Volley image loader

ImageView mImageView = (ImageView)findViewById(R.id.image_view);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(mImageView);
Muhamet Aljobairi
  • 1,627
  • 4
  • 19
  • 25
0

I can recommend a different way that works like a charm: Android Query.

You can download that JAR file from here

AQuery androidAQuery = new AQuery(this);

As an example:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.

Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
0

@Nima.S-H

see this Library very useful with more option

https://github.com/nostra13/Android-Universal-Image-Loader

MilapTank
  • 9,988
  • 7
  • 38
  • 53
0

You have to use lib for showing image. Use below lib :-

  1. https://github.com/koush/UrlImageViewHelper
  2. https://github.com/nostra13/Android-Universal-Image-Loader

for more info see below link :-

Lazy load of images in ListView

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

I recommend you to use LruCache, it improves a lot the performance loading images in ListViews.
Caching Bitmaps

Here an example showing the difference.

ann
  • 576
  • 1
  • 10
  • 19
adboco
  • 2,840
  • 1
  • 21
  • 21
0

I will recommend picasso library for image loading with OkHttp for speedy network communication. here you can find example of this. https://github.com/AnawazInc/OkHttp-Picasso-Example-Android

Ahmed Nawaz
  • 1,634
  • 3
  • 21
  • 51