Android ListView with AsyncTask image issue

187 views Asked by At

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);
    }
}
1

There are 1 answers

2
Robin On

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.