0

Hi and happy new year!

I'm trying to download images from internet and put them into different ImageViews. The ImageViews are dynamically created as the user scrolls. When user arrives to the bottom of the scrollview, I load 10 images more.

The images are loading ok, but when i have a lot of images I get a java.lang.OutOfMemoryError.

I know the problem is that I have a lot of images consuming a lot of memory, so... what's the way to go on my scenario?

Thanks!

Chux
  • 1,196
  • 1
  • 9
  • 24

2 Answers2

2

Try using a ListView instead of a ScrollView. Then you can use a lazy loading technique like Universal Image Loader. The ListView utilizes view recycling which will be easier on your memory, and you can also cache images using the image loader. This library also has a few options for memory management as well.

Wenger
  • 989
  • 2
  • 12
  • 35
  • I need two image colums (take pinterest app as example or what i'm doing). Do you think listview is suitable for that? – Chux Jan 08 '13 at 18:51
  • Do the columns scroll seperately or does the entire layout scroll as one list? – Wenger Jan 08 '13 at 19:03
  • they need to scroll as one single list – Chux Jan 08 '13 at 19:21
  • Ok. Are all the images the same height? Meaning, could you fit two images side-by-side into one list item. – Wenger Jan 08 '13 at 19:41
  • No they don't have the same height. They have the same width, but the height is unknown. Take this (min 1 sec 10) as example: http://www.youtube.com/watch?v=BOVX6_QzEmM – Chux Jan 09 '13 at 12:43
  • http://stackoverflow.com/questions/12342419/android-scrolling-2-listviews-together Check that out. If you want to use the memory management benefits of ListView while using two separate ListViews, scrolling together, you'll have to mess with intercepting touch or motion events. – Wenger Jan 09 '13 at 14:12
  • I think i'm trying to do something a bit complicated for a beginner like me. Thanks for the answer and for the help! – Chux Jan 09 '13 at 15:03
0

You are going to have to keep track of the images you have loaded and start recycling them when they are out of view.

You might find the LruCache a valuable tool. There was a good talk at IO12 "Doing More With Less: Being a Good Android Citizen" that went over lots of memory issues and includes some discussion on how to use the LruCache starting around the 4 minute mark.

Scott W
  • 9,742
  • 2
  • 38
  • 53