1

I'm using the popular lazy loading class found here:http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012

The problem I'm having is that I'm using this class in multiple activities etc... navigating back and forth between activities(loading images) eventually crashes my app, giving me this error:

12-07 19:54:42.414: W/dalvikvm(1204): threadid=91: thread exiting with uncaught exception (group=0x4001b188)
12-07 19:54:42.424: E/AndroidRuntime(1204): Uncaught handler: thread Thread-47 exiting due to uncaught exception
12-07 19:54:42.434: E/AndroidRuntime(1204): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-07 19:54:42.434: E/AndroidRuntime(1204):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader.decodeFile(ImageLoader.java:124)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader.getBitmap(ImageLoader.java:78)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader.access$0(ImageLoader.java:73)
12-07 19:54:42.434: E/AndroidRuntime(1204):     at de.tablayoutexample.ImageLoader$PhotosLoader.run(ImageLoader.java:182)

The code below is part of the ImageLoader class and is, I suspect, the culprit. Originally, REQUIRED_SIZE is set at 70, which is way too small. I set it at 200 which makes the image quality better, but crashes the app sooner.

Shouldnt this lazy loading method be cleaning the images every time the user leaves an activity? It seems it is just adding onto the pile every single time I load more images in another activity.

I'm new to android programming, so perhaps someone could help me optimize this code.

 //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=200;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
Adam
  • 8,849
  • 16
  • 67
  • 131

1 Answers1

0

WeakHashMap may help u to cache bitmap. http://developer.android.com/reference/java/util/WeakHashMap.html

lesscome
  • 64
  • 2