Musterknabe
Musterknabe

Reputation: 6081

Speed up image download time

in my app I'm downloading images I get from an URL. The problem here is that it takes wayy too long. The images are compressed and only 9km big. I'm downloading 50 of them, so let's say 500kByte of images are being downloaded by the app. This should be pretty fast, right? Well, it isn't. I'm sometimes wating for 20 seconds until it's loaded. What is taking so long there? I have a pretty easy way to download the images. Here it is

int downloadingImages = getDownloadImageCount();
        System.out.println(downloadingImages);
        if(downloadingImages == 0){
            for(int c = downloadingImages; c < 50; c++){
                try{                    

                        bitmapArr[c]        = getBitmapFromURL(imageURLArr[c]);
                        setDownloadImageCount(50);
                        publishProgress(c);
                    } catch (FileNotFoundException f1){
                        Log.v("FileNotFoundException", "Bitmap konnte nicht geladen werden");
                } catch(NullPointerException e){

                } catch (IllegalStateException ie){

                } 
                setDownloadImageCount(50);
            }
        }

This is the getBitmapFromUrl function

public static Bitmap getBitmapFromURL(String src) throws FileNotFoundException {
    try {
        //Downloading the image
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        try{
            //Saving the image to a bitmap and return it
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
            //Catch Exception if file not found
        } catch(FileNotFoundException ffe){
            Log.v("FileNotFoundException", "getBitmapFromURLMethod");
            return null;
        }        
        //Catch Exception if error at input and output
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } 
}

I mean, what is taking so long there? How can I increase the speed. I will use cahing for images later, but still... I mean it's 500kbs and takes so long. Why is that?

Upvotes: 1

Views: 1424

Answers (3)

Kevin
Kevin

Reputation: 15934

The longest step in the process is probably sending a request to the server and waiting for it to respond, and you're repeating this step 50 times. Do you have the ability to modify the server? If so, consider adding an action that lets you download all 50 images at once (perhaps by putting them into a zip file, then unpacking them client-side).

Upvotes: 2

rstk
rstk

Reputation: 414

Try to wrap your InputStream to BufferedInputStream

BufferedInputStream bs = new BufferedInputStream(input);
Bitmap myBitmap = BitmapFactory.decodeStream(input);

Upvotes: 0

Durandal
Durandal

Reputation: 20059

Its completely natural that it takes a while. For each image you load from a URL a new HTTP-request is made (that is a request is sent to the server and it responds).

Thats 50 times the round-trip time over the network (not even including the time it may take the server to respond). You should cache the images locally once you have them retrieved.

Upvotes: 2

Related Questions