Search code examples
androidcontactsandroid-cursoradapter

Load contact photos in list view (with CursorAdapter) is very laggish


Im building a contact application. In the main activity i have a listview, where i show the contacts.

I'm using a CursorAdapter to handle tha cursor with the list

It all works well without photos.

However, when i try to use photos the scrolling becomes laggish.

Thc calling to open the input stream is very slow, even if the inputStream is null and there's no image to load:

public static Bitmap loadContactPhoto(ContentResolver cr, long  id) 
{
   Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
   InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);

   if (input == null) 
   {
       return null;
   }

   return BitmapFactory.decodeStream(input);
}

What can I do about it?


Solution

  • Are you loading the images in the UI thread.... If yes,you should not do it as this would block the thread and make the user experiance sluggish.

    You should probably learn on lazy loading of images....try googling it out....

    I would also not do database operations on the UI thread...eventhough some say it is ok to do small DB ops in UI thread...I would avoid it as much as possible...

    Check the below link:

    Lazy load of images in ListView