jwandscheer
jwandscheer

Reputation: 178

Android AsyncTask Download multiple Images

In my Android App, I have an AsyncTask to Download a Picture from the Web and Show it in the UI (in onPostExecute() I'm generating a new ImageView). How can I make an AsyncTask which is downloading more than one Image at the same Time, and show the single Images directly when they are downloaded, even when the others aren't ready?

This is my Code:

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


        @Override
        protected Bitmap doInBackground(Void... params) {

            Bitmap bitmap = null;
            bitmap = downloadBitmap("HERE's MY URL");


            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {


            ImageView image = new ImageView(context);
            image.setImageBitmap(result);   


            ((ViewGroup) planLinearLayout).addView(image);


        }


        }

        public Bitmap downloadBitmap(String url) {
            final AndroidHttpClient client = AndroidHttpClient
                    .newInstance("Android");
            final HttpGet getRequest = new HttpGet(url);

            try {
                HttpResponse response = client.execute(getRequest);
                final int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    Log.w("ImageDownloader", "Error " + statusCode
                            + " while retrieving bitmap from " + url);
                    return null;
                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        inputStream = entity.getContent();
                        final Bitmap bitmap = BitmapFactory
                                .decodeStream(inputStream);


                        return bitmap;
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            } catch (Exception e) {
                // Could provide a more explicit error message for IOException
                // or
                // IllegalStateException
                getRequest.abort();
                Log.w("ImageDownloader", "Error while retrieving bitmap from "
                        + url);
            } finally {
                if (client != null) {
                    client.close();
                }
            }
            return null;
        }

    }

Upvotes: 1

Views: 8135

Answers (4)

Eenvincible
Eenvincible

Reputation: 5626

Since you cannot make two requests at once, you can alternatively do the following:

  • First, in your activity, create your AsyncTask instance and execute it with an argument like "firstImage" and then in doInBackground(), simply check to see if that is the value of the argument passed and if so, download the first image. After the image is downloaded, save it somewhere in a local variable or whatever you want. Then return a string like "firstImage". Inside onPostExecute, just check the value of result and pass back the image to the activity which will update the UI.
  • Secondly, once you have updated the UI with the first Image, make a second Async call with a string like "secondImage" and repeat the same process as before and update the UI - this will add the second image to the UI. You won't need a library for this!

Upvotes: 2

Ajay S
Ajay S

Reputation: 48592

I recommend to you to use the Universal Image Loader library to download the images in android.

It download the image as well do some more good things like caching the images and manage the memory very tricky.

Update

If you do not want to cache the images then you can disable this feature using the Configuration in the Universal Image Loader library.

Library links : https://github.com/nostra13/Android-Universal-Image-Loader

Upvotes: 1

Niko
Niko

Reputation: 8153

I would recommend Picasso, there you can use skipMemoryCache() if you do not want images to be cached at all.

Upvotes: 1

FD_
FD_

Reputation: 12919

Just make use of onProgressUpdate(Progress...). Change your second generic type to Bitmap and call publishProgress() after you have finished loading an image.

Upvotes: 1

Related Questions