Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview items remove bitmaps from memory when the user scrolls

I am working on an android application. I have an activity that contains a list view.

The list view has lots of items and each contains a bitmap image.

Because the list is very big and contains a lot of images it is giving and out of memory error on some old devices when loading the bitmaps.

So I am looking for a way to do the following for old devices:

  • Load the bitmaps for the items that are in the view (or near it)
  • When the user scrolls down I load the bitmaps that should appear to him and remove the bitmaps that are now hidden.
  • So at anytime only the images that are in the view (or near it) are loaded in the Heap while the others are removed when they are no longer in the view.

Is there a way to do it?

Additional information:

I have followed every instructions and code on how to load Bitmap images efficiently (on the android documentation and on any site that I could find), I also made sure there is no memory leaks, I recycle every bitmap and make sure the GC works correctly and finally, on old devices I made the resolution of the images the smallest possible. But the size of the list is causing the out of memory to still appear on some devices.

So I am looking to know if I can do the above proposed solution.

Thanks a lot for any help

like image 632
Y2theZ Avatar asked Jan 09 '13 15:01

Y2theZ


1 Answers

You should consider looking at Sample Gallery App from Google I/O 2012. Everything you need is implemented there.

Video from Google I/O about gallery app

Source code is available on Google Code and it's an exclellent example of building a Gallery. You should also consider looking at Romain Guy's presentation (beginning) where he explains performance issues with ListView and teaches to use adapters efficiently. (ViewHolder pattern and reusing views).

Here's a RecycleListener for GridView (Available from API level 1). A RecyclerListener is used to receive a notification whenever a View is placed inside the RecycleBin's scrap heap. This listener is used to free resources associated to Views placed in the RecycleBin.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.album);

    mAdapter = new PhotoAdapter(this);

    mGridView = (GridView) findViewById(android.R.id.list);
    mGridView.setAdapter(mAdapter);

    mGridView.setRecyclerListener(new RecyclerListener() {
        @Override
        public void onMovedToScrapHeap(View view) {
            // Release strong reference when a view is recycled
            final ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
            imageView.setImageBitmap(null);
        }
    });

    // Kick off loader for Cursor with list of photos
    getLoaderManager().initLoader(LOADER_CURSOR, null, mCursorCallbacks);
}
like image 124
Taras Leskiv Avatar answered Oct 12 '22 15:10

Taras Leskiv