1

I have a large list of objects, all of whom have a path to their image "ex. http://www.google.com/image.jpg" and I need to download the image and save the drawable to the object..

I was using AsyncTask, but even if I use my own threads I always end up with 'OutOfMemoryError' at some arbitrary point in the list. The images are never larger than 82Kb (Is this too big for android tablets?) in size, but I think the sheer number of images is causing the process as a whole to fail.

Here is what I'm currently doing.

class DownloadImageTask extends AsyncTask<ArrayList<Item>, Void, Void> {

private static int num =1;

@Override

protected Void doInBackground(ArrayList<Item>... items) {

    try {

        if(items.length == 0)

            return null;

        HttpURLConnection connection;

        InputStream input;

        for(ArrayList<Item> itemlist : items) {

            for(Item i : itemlist) {

                Log.d(JusTouchMenu.TAG,"[Item]Image request to url:"+i.getImagePath());

                try {

                    connection = (HttpURLConnection)new URL(i.getImagePath()).openConnection();

                    connection.setRequestProperty("User-agent","Mozilla/4.0");

                    connection.connect();

                    input = connection.getInputStream();

                    i.setImage(new BitmapDrawable(BitmapFactory.decodeStream(input)));//Requires a drawable

                    connection.disconnect();

                } catch(Exception e) {Log.e(JusTouchMenu.TAG,"[Item]Unable to download image @ '"+i.getImagePath()+"'",e);}

                Log.v(JusTouchMenu.TAG, "[Item]Image decoded @ '"+i.getImagePath()+"' #"+num++);

                for(Tag pt : i.tags()) {

                    Log.d(JusTouchMenu.TAG,"[Item->Tag]Image request to url:"+pt.getImagePath());

                    try {

                        connection = (HttpURLConnection)new URL(pt.getImagePath()).openConnection();

                        connection.setRequestProperty("User-agent","Mozilla/4.0");

                        connection.connect();

                        input = connection.getInputStream();

                        pt.setImage(new BitmapDrawable(BitmapFactory.decodeStream(input))); //Requires a drawable

                        connection.disconnect();

                    } catch(Exception e) {

                        Log.e(JusTouchMenu.TAG,"[Item->Tag]Unable to download image @ '"+i.getImagePath()+"'",e);

                    }

                    Log.v(JusTouchMenu.TAG, "[Item->Tag]Image decoded @ '"+pt.getImagePath()+"' #"+num++);

                }

            }

        }

    } catch(Exception e) {

        Log.e(JusTouchMenu.TAG,"Error decoding image inside AsyncTask",e);

    }

    return null;

}

Thanks!

steve-gregory
  • 7,396
  • 8
  • 39
  • 47
  • Where and how are you using those images? Are you sure you need to load all of them at once? Images should be load just when they are needed. – Cristian Oct 24 '11 at 20:10
  • Thank you for making this distinction. I was about to tell you how I needed all of the images, but realized that the subset of images in an object will not be used immediately and can be picked up at a later time. However, this doesn't change the question. Let us assume that I did need all of these images immediately, how would I go about ensuring they were all downloaded without hitting an OutOfMemoryError? – steve-gregory Oct 24 '11 at 20:15

1 Answers1

0

How many of these images are you trying to get at once? How are you using them?

You should probably consider using the capabilities of Gallery or GridView and thereby only be loading the images that are in view at the moment. You can even get fancy and cache them so scrolling is nice and smooth. Trying to load all of the images into an ArrayList when youre not likely to be able to display them all at once is not a mobile friendly approach.

Here are some decent references to caching approaches but you should probably get started with a basic gallery or grid view loading as needed and then add the caching on.

Lazy load of images in ListView

http://code.google.com/p/libs-for-android/wiki/ImageLoader

Community
  • 1
  • 1
mmeyer
  • 3,598
  • 1
  • 19
  • 22