0

What is the best way to handle a very long list view with bitmaps in a low memory device? I'm getting crashes after I scroll a bit... I don't want to loose image quality... I was thinking about loading only the number of items which is possible for the available memory, but this is obviously a bad solution. Do I have to recycle manually the bitmaps, in getView(), according to the position...? Is this the way to handle this?

I'm targeting API 7.

Thanks in advance.

P.S. The bitmaps are loaded from a remote server using HTTP.

User
  • 31,811
  • 40
  • 131
  • 232
  • Just in case you haven't read it yet, [this article on the Android Developer site](http://developer.android.com/training/displaying-bitmaps/index.html) covers a lot of the general issues related to this problem. It might not be specific enough to help you though (since you mention you have a low memory device as target). – Joe Jul 15 '12 at 12:58
  • I already read that, but at least currently it doesn't give me a concrete idea about how to solve this. – User Jul 15 '12 at 13:02

2 Answers2

0

Load the Bitmap directly from the SD Card. Do not keep it in memory in some sort of bitmap cache.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Sorry, I forgot to mention that the bitmaps are loaded from a server using http requests. – User Jul 15 '12 at 10:26
  • store it inside the sdcard. Than load accordingly to its position – Blackbelt Jul 15 '12 at 10:46
  • storing in the SD card doesn't solves the question, I still have the same issue. After scrolling a while they will be loaded in RAM. so the question how do I unload / manage them is still the same. – User Jul 15 '12 at 10:51
0

Did you recycle your views in getView() method? If not, do the next thing:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if(mInflater == null){
    mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    View viewItem = convertView;
    if(viewItem == null)
        viewItem = mInflater.inflate("YOUR_LAYOUT", null);


//Continue with your code but use viewItem as the view of the list view line, that way you don't always create new instance of it. This is what helped me in the same problem.

EDIT: Check the next posts, may be it can help you.

How do I do a lazy load of images in ListView

Multithreading For Performance

Community
  • 1
  • 1
Ofir A.
  • 3,112
  • 11
  • 57
  • 83
  • This doesn't help with the memory consumption of the bitmap data... It will recycle the holder, but the new fresh bitmap will always be loaded into memory. – User Jul 15 '12 at 10:49
  • @Ixx You right, but this is also a problem. May Be first download all the images, store them is your sd-card, and then use them? – Ofir A. Jul 15 '12 at 10:51
  • It doesn't matter if I get them from a remote server of from the SD card (concerning memory, at least, the question is not about speed)... the point is they will be loaded in memory when I scroll and then I run out of memory. – User Jul 15 '12 at 10:53
  • @Ixx I can tell you that I had the exact same problem, and this is how I solved it. Not recycling the holder is causing memory problem and you can check this in DDMS in the HEAP section, see how your allocated memory rising when you only scroll the list. – Ofir A. Jul 15 '12 at 10:55
  • I am already recycling the holder... exactly as you posted it. – User Jul 15 '12 at 11:10