Search code examples
androidimagegalleryout-of-memoryrecycle

Android gallery of drawables


I'm creating a simple Gallery of drawables - each of them is almost the size of a screen, so they require quite much memory. For each entry I'm creating a custom LinearLayout with ImageView and TextView for the title. As most of you know, android Gallery doesn't recycle views so it gallery will crash easily on low-memory phones (after loading 4 drawables on 16mb ram limit, in my case).

Here's the simple question - how do you implement such gallery, so it won't run out of the memory? How do you recycle these images? A working code example would be great.

Few notes:

  • inSampleSize isn't a way to go, I can't scale these images down

  • Calling recycle() on Drawable's loaded from resource is impossible, as it will crash on Android 4.0+ (it will recycle the drawable in their internal cache)

  • Don't ask me to post the code, as there is no.


Solution

  • You shouldn't be using Gallery because it's deprecated. Especially since there isn't any code written so far. The documentation suggests using a HorizontalScrollView or ViewPager.

    I feel a ViewPager is what your looking for because it will only keep at most 3 pictures in memory and handels all the recycling for you. Here is a post with more information about how to implement one android viewPager implementation