Search code examples
androidlistviewcustom-adapter

Android load images from URL into custom listview adapter without Picasso


I am trying to load images from a url and I have used Picasso, however I'd like to know how to do it without an external library if possible. I know I have to get an Asynctask going but I'm not sure how to implement it.

This is my getview code

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    if(position==0){

        NewsObj currNews = news.get(position);
        DataHandler dh;
        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.firstnews,parent,false);
            dh = new DataHandler();
            dh.newsTitle = (TextView)row.findViewById(R.id.newsTitle);
            dh.newsDate = (TextView)row.findViewById(R.id.newsDate);
            dh.newsIcon = (ImageView)row.findViewById(R.id.newsIcon);
            row.setTag(dh);
        }else{
            dh = (DataHandler)row.getTag();
        }
        NewsObj no = (NewsObj)this.getItem(position);
        new AsyncDownloadTask().execute(row,no.getImgurl());
        dh.newsTitle.setText(no.getTitle());
        dh.newsDate.setText(no.getDate());

    }else{

        NewsObj currNews = news.get(position);
        DataHandler dh;
        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.newslist,parent,false);
            dh = new DataHandler();
            dh.newsTitle = (TextView)row.findViewById(R.id.newsTitle);
            dh.newsDate = (TextView)row.findViewById(R.id.newsDate);
            dh.newsIcon = (ImageView)row.findViewById(R.id.newsIcon);
            row.setTag(dh);
        }else{
            dh = (DataHandler)row.getTag();
        }
        NewsObj no = (NewsObj)this.getItem(position);
        new AsyncDownloadTask().execute(row,no.getImgurl());

        dh.newsTitle.setText(no.getTitle());
        dh.newsDate.setText(no.getDate());

    }
    return row;
}


 private class AsyncDownloadTask extends AsyncTask<Object, String, Bitmap>{

    private View view;
    private Bitmap bitmap = null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if(bitmap!=null&&view!=null){
            ImageView newsIcon = (ImageView)view.getTag(R.id.newsIcon);
            newsIcon.setImageBitmap(bitmap);
        }
    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        view = (View)params[0];
        String uri = (String)params[1];
        try{
            InputStream inputStream = new URL(uri).openStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        return bitmap;
    }
}

This is my async task

UPDATE : Encountering null pointer exceptions

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

Solution

  • Simply you can use java.net.URL for loading an image into ImageView like

        ImageView loadedImage;
    
        @override
        protected Bitmap doInBackground(String... url) {
            String URL_OF_IMAGE = url[0];
            Bitmap bitmap = null;
            try {
                InputStream in = new java.net.URL(URL_OF_IMAGE).openStream();
                bitmap= BitmapFactory.decodeStream(in);
            } catch (Exception e) {
            }
            return bitmap;
        }
    
        protected void onPostExecute(Bitmap result) {
            loadedImage.setImageBitmap(result);
        }
    

    But notice that you should have extend AsyncTask that should have first parameter as String and last as Bitmap like

    private class DowmloadImage extends AsyncTask<String, Void, Bitmap>