Search code examples
androidlistviewuniversal-image-loader

android load image from url


I use the universal image loader library to load about 50 image from url to listview. Here is the binderdata.

public class BinderDataImg extends BaseAdapter {
    static final String KEY_IMG = "img";
    LayoutInflater inflater;
    List<HashMap<String,String>> imgHashmap;
    ViewHolder holder;
    public BinderDataImg() {
        // TODO Auto-generated constructor stub
    }
    public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
        this.imgHashmap = map;
        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return imgHashmap.size();
    }
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null){
            vi = inflater.inflate(R.layout.list_img, null);
            holder = new ViewHolder();
            holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
            vi.setTag(holder);
        }
        else{
            holder = (ViewHolder)vi.getTag();
        }
        String uri = imgHashmap .get(position).get(KEY_IMG);
        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true).build();
        imageLoader.displayImage(uri, holder.iv_img, options);
        return vi;
    }
    static class ViewHolder{
        ImageView iv_img;
    }
}

The activity.

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);

Almost, but not all images load and in logcat there's an OutOfMemoryError. How can this be fixed?

Update:

This is Binderdata from the Picasso library.

public class BinderDataImg extends BaseAdapter {
    static final String KEY_IMG = "img";
    LayoutInflater inflater;
    List<HashMap<String,String>> imgHashmap;
    ViewHolder holder;
    public BinderDataImg() {
        // TODO Auto-generated constructor stub
    }
    Activity act;
    public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
        this.imgHashmap = map;
        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                this.act = act;
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return imgHashmap.size();
    }
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null){
            vi = inflater.inflate(R.layout.list_img, null);
            holder = new ViewHolder();
            holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);
            vi.setTag(holder);
        }
        else{
            holder = (ViewHolder)vi.getTag();
        }
        String uri = imgHashmap .get(position).get(KEY_IMG);
        Picasso.with(act).load(uri).into(holder.iv_img);
        return vi;
    }
    static class ViewHolder{
        ImageView iv_img;
    }
}

This loads the images ok, but when scrolling up or down the images reload again.


Solution

  • Ok here is my implemented code using picasso and its working smoothly for me

    public class MyFragmentAdapter extends ArrayAdapter<Video> {
        private Context mContext;
        private LayoutInflater mInflater;
        private Bitmap mBitmap;
    
        public MyFragmentAdapter(Context context, int resource, List<Video> objects) {
            super(context, resource, objects);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.mContext = context;
        }
    
    
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.item_gallery, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.iv_thumbImage);
                holder.pb = (ProgressBar) convertView.findViewById(R.id.iv_progressbar);
                convertView.setTag(holder);
    
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.pb.setVisibility(View.VISIBLE);
            String url="Url here";
                Picasso.with(mContext).load(thumbnail_path).placeholder(R.drawable.icon_video).into(holder.imageview, new Callback() {
                    @Override
                    public void onSuccess() {
                        holder.pb.setVisibility(View.INVISIBLE);
                    }
    
                    @Override
                    public void onError() {
                        holder.pb.setVisibility(View.INVISIBLE);
                    }
                });
            }
            return convertView;
        }
    
        public static class ViewHolder {
            ImageView imageview;
            ProgressBar pb;
        }
    }