URL and Link repository

Friday, March 15, 2019

Android Picasso vs s33me code

I spent several hours tweaking my own loading of images into an Android ListView. The problem was that the images would not load until I scrolled the screen and even then the lag was unacceptable.

My program performed the following:

  • Download a non indexed list of geotag data that includes the image URL into ArrayList rowdatalist
  • Loop through the rows in rowdatalist and Async create the bitmap from the image URL in each row
  • The customRowAdapter for the ListView will have a ViewHolder that sets the holder.imageView from the bitmap decoded from the URL.
  • Essentially, this produces statements like the following:
    holder.imageView = (ImageView) convertView.findViewById(R.id.row_image);
    holder.imageView.setImageBitmap(rowdata.getBitmap());
    This is from inside the object rowdata

    public Bitmap getBitmap() {
    if (bitmapurl != null && !bitmapurl.equals("")) {
    new ImageLoadAsync(context).execute(bitmapurl);
    } else {
    // default image if nothing from url
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.war4a);
    }
    return bitmap;
    }

    This is the background thread portion of the Async call:
    @Override
    protected Bitmap doInBackground(String... urls) {
    String surl = urls[0];
    Bitmap bm = null;
    try {
    URL imageUrl = new URL(surl);
    bm = BitmapFactory.decodeStream(imageUrl.openStream());
    } catch (IOException e) {
    e.printStackTrace();
    bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.war4a);
    }
    return bm;
    }

    This is just a portion of the cumbersome code. As an experienced programmer, I should have known not to reinvent the wheel, but I just could not help myself

    At the end of the day, this line of code replaced all that nonsense

    Picasso.with(context).load(rowdata.getBitmapUrl()).into(holder.imageView);