简体   繁体   中英

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.

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;
    }
}

Try with Aquery Image loading lib,that worked for me

public class CustomAdapterAccept extends BaseAdapter {

        private Context context;
        private ArrayList<HashMap<String,String>> listData;


        private static final String TAG_NAME="brandname";
        private static final String TAG_IMAGE="brandimg";
        public CustomAdapterAccept(Context context,ArrayList<HashMap<String,String>> listData) {
            this.context = context;
            this.listData=listData;
            aQuery = new AQuery(this.context);
        }

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

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item_cat, null);
                holder.propic = (ImageView) convertView.findViewById(R.id.brndimage);
                holder.txtproname = (TextView) convertView.findViewById(R.id.txtcatname);


                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtproname.setText(listData.get(position).get(TAG_NAME));

            aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE), true, true, 0, R.drawable.ic_launcher);


            aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

            // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
            return convertView;
        }
        class ViewHolder{
            ImageView propic;
            TextView txtproname;
            TextView txtproid;
            TextView txtprofilecast;
            TextView txtprofileage;
            TextView txtprofileplace;
        }

    }

For more see this

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM