Android contact image placeholder in listview

451 views Asked by At

I have an activity with a listview and a custom adapter. Retrieving the contact information and the image is not the problem. I can not display an alternative image if there is not one for the contact.

Checking if the URI is NULL does not work for me.

I am retrieving the URI of the android contacts and the resulting URI have the format

content://com.android.contacts/contacts/XXX/photo

where XXX is a number. The resulting URI is only NULL for default contacts added like emergency numbers, so checking for NULL to replace the image with a placeholder image works only on those numbers. For all other contacts that have not set an image i want to check the filesize of the contact image.

File f = new File(uri.getPath());
long size = f.length();
String str = Long.toString(size);

Using the above commands results are always 0, it doesn´t matter if there is a photo or not.

How can i check the filesize correctly? or how can i check if the resulting photo is empty.

Thanks for helping

3

There are 3 answers

0
Ingolf Krauss On BEST ANSWER

The idea to check if the file exists led me to this post where it was answered

detect if contact has photo

.getDrawable() == null

gets the correct result.

thanks for all comments

0
Adam Fręśko On

I encourage you to use Facebook Fresco library. You will be able to provide place holder image aswell as error image for image view

0
gkondati On

Use any famous image loaders like Universal image loader or picasso lib. This libs will handle every thing, its have some methods for putting error images. And you can directly put your contact URl in this libs. They will support all kinds of url. you can check here