0

Currently i'm loading images from a url and it is taking way to long and i cannot work out why, sometimes taking longer than 60 seconds to get images that aren't really that big.

My Code:

Get image async task:

public class GetImageAsyncTask extends AsyncTask<Void, Void, Bitmap> {

String url;
OnImageRetrieved listener;
ImageView imageView;
int height;
int width;

public GetImageAsyncTask(String url, ImageView imageView,OnImageRetrieved listener, int height, int width) {
    this.url = url;
    this.listener = listener;
    this.imageView = imageView;
    this.height = height;
    this.width = width;
}

public interface OnImageRetrieved {
    void onImageRetrieved(Bitmap image, ImageView imageview, String url);
}

protected Bitmap doInBackground(Void... params) {

    Bitmap image = null;

    try {
        image = ImageUtilities.decodeSampledBitmapFromUrl(this.url, this.width, this.height);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return image;
}

    protected void onPostExecute(Bitmap result) {
        this.listener.onImageRetrieved(result, this.imageView, this.url);
    }
}

 public static Bitmap decodeSampledBitmapFromUrl(String url, int reqWidth, int reqHeight) throws IOException {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeStream(new java.net.URL(url).openStream(), null, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeStream(new java.net.URL(url).openStream(), null, options);
}

Getting sample size:

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

These methods are used because of memory complications that can arise if not, but the time it seems to take is just to long. Is there some very heavy computation that i'm just not seeing or?

Joe Maher
  • 5,354
  • 5
  • 28
  • 44
  • so how big they are on the server side? what is the value of `options.outHeight` and `options.outWidth` ? – pskink Oct 26 '15 at 08:52
  • The images on s3 range from 300 to 1100kb, so i mean, not exactly massive. Width can be anywhere between 500-2000, height 400- 1200. Think ive come across another issue, my adapter getView is getting called way to many times which is resulting in literally 100's of calls to my getIMageAsyncTask – Joe Maher Oct 27 '15 at 00:46

2 Answers2

1

you can use Picasso or volly library to load image.I suggest volly to use because its introduce by google itself.

akhil batlawala
  • 1,066
  • 1
  • 10
  • 30
  • Im marking this as correct, because honestly Picasso has saved me 10's of hours, I have no idea what makes it so hard to deal with images in android burt picasso definitely solves the problem. – Joe Maher Nov 11 '15 at 05:54
0

So the problem stems from the array adapter and the fact that getView() can be called 100's of times which can be close to 100mb of data is being downloaded simultaneously.

So as a temp fix for this situation i implemented a global LruCache singleton, that i first check before starting the async task.

This is clearly not ideal but it will have to do for now. Im sure there are better solutions out there and i would love to hear them if anyone has one to offer.

Joe Maher
  • 5,354
  • 5
  • 28
  • 44