0

I have a ListView which shows some certain contacts from phone book. I use this https://stackoverflow.com/a/10235381/1809507 as Cache class and use AsyncTask.

When a contact does not have a photo I use a default one. I set the default one inside XML. When I use the following methods to set the contact photo only if it exists and I scroll the listview those contacts that do not have photo start to get other contacts photo. If I get from retrieveContactPhoto method the default photo as Bitmap and not null and assign to imageview, it works great but I think this is not a good solution when there are many contacts without photos because I would cache the same default photo multiple times. Or I am wrong?

@Override
    protected Bitmap doInBackground(Void... args)
    {
        Bitmap bm = HelpClasses.retrieveContactPhoto(contextInput, HelpClasses.fetchContactIdFromPhoneNumber(contextInput, phoneNumber));
        if(bm != null)
        {
            synchronized (cache)
            {
                cache.put(phoneNumber, bm);
            }

        }
        return bm;
    }

@Override
protected void onPostExecute(Bitmap result)
{
    if(mView != null && result != null)
    {
         mView.setImageBitmap(result);
    }
}
Community
  • 1
  • 1
edoniti
  • 107
  • 12
  • http://samir-mangroliya.blogspot.in/p/android-read-contact-and-display-in.html – Rahul Gupta Nov 29 '13 at 02:57
  • As you can see on my code I already have the method to fetchContactIdFromPhoneNumber and then retrieveContactPhoto from contact ID. – edoniti Nov 29 '13 at 03:08

1 Answers1

0

I think you are looking for this library: https://github.com/nostra13/Android-Universal-Image-Loader

Or, you might want a more "google" one: https://code.google.com/p/libs-for-android/wiki/ImageLoader

You can leave all the image binding, fetching, caching stuff to the library.

Robin
  • 10,052
  • 6
  • 31
  • 52
  • Thanks a lot, but this libraries will cache the default photo too. Which I think is unnecessary, I want to let the default photo from XML not assign again from cache or sdcard. – edoniti Nov 29 '13 at 17:25
  • The default image is cached in the memory, and all the image view will refer to the same instance. – Robin Nov 30 '13 at 03:47