3

In my app we are using a gallery for photos , taken from android dev (http://developer.android.com/resources/tutorials/views/hello-gallery.html) .It loads all the photos from /sdcard/DCIM (all subdirectories). Althought we have made it resample the shown image to use less memory, it still crashes if the users has a lot of pictures or scrolls too fast.

Are there any suggestions on how to make gallery not crash the application??

(Shouldn't google have provided us with a gallery "out of the self" that takes care of all these issues?)

Panos
  • 7,227
  • 13
  • 60
  • 95

2 Answers2

1

The problem with the standard android gallery is that it will not use view recycling. You can do whatever you want cache images etc. The gallery itself will keep all imageviews that are added to the gallery in memory. This may lead to a fast scrolling gallery, but it also leads to a lot of out of memory exceptions.

A good solution for this is to use the Compatibility Library (available since Api Level 4) and the Viewpager that can be found in there. It will give you a very maintainable gallery that is much more usable (no more jumping of images etc.) and if you are using a FragemtSatePagerAdapter the fragments that are not currently shown in the gallery may be destroyed, if memory gets tight.

Be sure to implement the lifecycle of the fragments correct. This means:

  • A fragment needs an empty constructor.
  • Save the fragment data in onSaveInstanceState. In your case you may want to save the path to the image.

If you are not using fragments yet using the viewpager may look like a lot of work. But it is worth it. You and your user will have a much nicer app afterwards.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • my app has a requirement of min api 7. Will I be able to use it? I'm not using fragments since the app is intended to cell phones and not tablets. – Panos Jan 30 '12 at 13:32
  • You can use the compatibillity library to use the viewpager. This makes the Viewpager available from Android 1.6 upwards. – Janusz Jan 30 '12 at 13:38
-1

Have you considered using a hashmap with soft references to all the images and loading them on demand if not available? It makes no sense to have tons of images in memory when they are available on the SD card.

private static final ConcurrentHashMap <String, SoftReference<Bitmap>> sBitmapCache =
    new ConcurrentHashMap <String, SoftReference<Bitmap>>();

public static Bitmap getBitmapCache(String imageUrl) {
    SoftReference<Bitmap> reference = sBitmapCache.get(imageUrl);
    Bitmap bitmap = null;
    if(reference != null) {
        bitmap = reference.get();
    } else {
    // get from SD
    }

    return bitmap;
}

Oh another edit: You can also scale the image down in quality, you don't need a full quality image on 20x20 px size area :)

ethan
  • 780
  • 7
  • 20
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • SoftReferences in Android will be cleared the moment that content is not hard references. This is not an option. See http://code.google.com/p/android/issues/detail?id=20015 for more information. – Janusz Jan 30 '12 at 13:17