0

I'm trying to implement an image gallery in android. The code based on http://www.mobisoftinfotech.com/blog/android/android-gallery-widget-example-and-tutorial/ and i've changed some details.

I'm using WeakReference and it seems, that when i've too many bitmaps, the garbage collector destroys my weakreferences. How can i handle this?

I get my bitmaps via this function:

    public static WeakReference<Bitmap> getBitmap(String imageName, int width,
        int height) {
    String pathToImage = getPathToImage(imageName);
    Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathToImage, options);

    /*
     * Calculate inSampleSize
     */
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    WeakReference<Bitmap> scaledBitmap = new WeakReference<Bitmap>(
            BitmapFactory.decodeFile(pathToImage, options));
    return scaledBitmap;
}

And i've taken the Solution 320x480, so i think it is not this big...

When the gallery has more than 3 pictures, some of them aren't displayed.

Is the gallery-tutorial not that good? Are there other ways to implement this?

Thank you!

Frame91
  • 3,670
  • 8
  • 45
  • 89

1 Answers1

1

Instead of using soft references, you should take a look at the lrucache class (it became available in honeycomb, but is part of the android-support library.

You can read more about it here : http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html It's pretty convenient : use this and you won't have to handle the memory yourself with weak references :-)

Redwarp
  • 3,221
  • 3
  • 31
  • 44
  • Thanks for your advice! Now i dont have the memory-leak anymore... but still have problems with showing bitmaps effieciently... (dont reload everytime i change the current bitmap :( [see](http://stackoverflow.com/questions/14775940/memory-leaks-image-gallery-android-how-can-other-applications-handle-it) ) – Frame91 Feb 08 '13 at 15:38
  • Just to be sure : are you downloading images from internet ? Or are you using pre-packaged images ? – Redwarp Feb 08 '13 at 16:03
  • when i open the gallery, the images are on my disk ;) (downloaded it before) – Frame91 Feb 08 '13 at 16:12
  • Are you using an async task to load the image, or doing it straight from the getView ? Also, when you say "some images are not displayed, are you talking about the preview, or the full screen image ? – Redwarp Feb 08 '13 at 16:33
  • Well, i've opened a new thread ;) [link](http://stackoverflow.com/questions/14775940/memory-leaks-image-gallery-android-how-can-other-applications-handle-it) Can you look into this? ;) They're loading in async-tasks. Some Images are not displayed, because the cache has destroyed them and now they have to be reloaded again... Thats not the behaviour i like :( Some applications like whatsapp can show many images without any problems ... :( – Frame91 Feb 08 '13 at 18:52