1

I have a problem with a ListActivity, it loads a search result, 5 items at a time, and when it reach about 45 items, it throws an out of memory exception.

I load images from web server asyncronically. If I comment this, i can scroll to the bottom of the list (aprox 58 items) without problem. I Know i should call bitmap.Recycle() to gc the bitmaps i no longer use, but I dont find when i should call that.

class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView>  imageViewReference;
        String                                  url;

        public ImageLoadingTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
            imageViewReference.get().setImageDrawable(null);
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            if (isCancelled()) {
                result = null;
            }
            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {                        
                    imageView.setImageBitmap(result);
                    //result.recycle();
                }
            }
        }

        @Override
        protected void onPreExecute() {
            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageResource(R.drawable.icon);
                }
            }
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            url = params[0];
            return Application.get(getContext()).loadBitmap(url);
        }
    }

As you see in the onPostExecute method. if I call recycle right after the image is asigned to the imageview, it thros an exeption of tying to use a recycled image.

Anybody could help?

Thanks in advance, and sorry for my english :P

1 Answers1

0

I suggest lazy loading the images. Which means downloading them to the SD card first and then loading them as needed into the list. Checkout this stackoverflow post for an extensive discussion on how to do this.

Community
  • 1
  • 1
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102