android load image from url
<p>I use the <code>universal image loader</code> library to load about 50 image from url to <code>listview</code>.
Here is the binderdata.</p>

<pre><code>public class BinderDataImg extends BaseAdapter {
    static final String KEY_IMG = "img";
    LayoutInflater inflater;
    List&lt;HashMap&lt;String,String&gt;&gt; imgHashmap;
    ViewHolder holder;
    public BinderDataImg() {
        // TODO Auto-generated constructor stub
    }
    public BinderDataImg(Activity act, List&lt;HashMap&lt;String,String&gt;&gt; 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;
    }
}
</code></pre>

<p>The activity.</p>

<pre><code>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);
</code></pre>

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

<p><strong>Update:</strong></p>

<p>This is Binderdata from the <code>Picasso</code> library.</p>

<pre><code>public class BinderDataImg extends BaseAdapter {
    static final String KEY_IMG = "img";
    LayoutInflater inflater;
    List&lt;HashMap&lt;String,String&gt;&gt; imgHashmap;
    ViewHolder holder;
    public BinderDataImg() {
        // TODO Auto-generated constructor stub
    }
    Activity act;
    public BinderDataImg(Activity act, List&lt;HashMap&lt;String,String&gt;&gt; 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;
    }
}
</code></pre>

<p>This loads the images ok, but when scrolling up or down the images reload again.</p>