The default Gallery widget on Android does not recycle views - everytime the view for a new position is called the widget always calls the getView
method of the adapter with convertView
set to null.
As you scroll backwards and forwards this ends up in lots of views being created, which the recycler component that the Gallery stores them in does not seem to recycle them quickly enough leading to an OOM situation.
You can test this easily with a few large-ish images as your gallery items, but just a TextView will cause it in the end. Put a log statement with a counter in the getView
method of your adapter also to see how many new views are created.
Does a third-party widget which behaves like a Gallery but that also implements view recycling exist?
I used patch from http://code.google.com/p/android/issues/detail?id=3376#c19
Actually there is an alternative, though I haven't personally tested it:
https://github.com/falnatsheh/EcoGallery
My solution was, in the end, going with @CommonsWare's suggestion to modify the Gallery source code. This is also required copying the following files:
AdapterView
AbsSpinner
but these are pretty simple.
After that I modified code to do the following:
With these modifications my counter in my
newView
method in my adapter reached... 7.Here is the code (Placed in the public domain 2013/08/07 under http://en.wikipedia.org/wiki/WTFPL)
Super late to the party, but I've modified EcoGallery to do a few more things (and avoid some crashes).
I've called it TimelineGallery and it's the same crap as the Gallery, but it can do smooth scroll and doesn't do weird stuff when images are loaded asynchronously.
To demonstrate it, the sample uses Picasso and PullToRefresh.
The original code, copyright and such belongs to Google so blame them for making such a crappy widget.
Final note: I do not recommend using the gallery, it's old, buggy, hacky and will probably never be maintained. The problem is not about fixing its bugs, the problem is that the whole architecture of the Gallery is wrong and as such, fixing it is not possible without introducing more hacks.
Google realized this and deprecated it. Use a ViewPager or a HorizontalScrollList and deal with the limitations of each.
If you still want to go ahead and use this "gallery", feel free, it works, but it may crash your app and may frustrate you.
Another quicker WorkAround for the OutOfMemory Issues, is to try/catch the code where you decode the image and if the OutOfMemory-exception is thrown, you try to decode it with smaller Resolution again..
something like this:
Ofcourse it is now possible, that you will see different resolutions of the same picture at different times - But at least your Gallery will not crash anymore and you allways show the highest resolution you can.