Hardy
Hardy

Reputation: 1539

Phonegap android handle null contact image

I use Phonegap (2.5) to get Contact image from android (4.0) to make a list contacts with avatar and name.

Contact avatar get fine as url data (example data: content://com.android.contacts/contacts/189/photo) but I don't know how to handle if that url is point to a null image ( as it will show up a blue square with question mark inside in that contact item did not have avarta setup ). If image is null or not setup then it should be my default image url.

...
navigator.contacts.find(
  ['id', 'name', 'phoneNumbers', 'photos', 'displayName'],
  function(deviceContacts) {     
  for (var i = 0; i < deviceContacts.length; i++) {
        var deviceContact = deviceContacts[ i ];
        if (deviceContact.photos !== null){
            img = deviceContact.photos[0].value; //url to image
            my_img_item.setSrc(img);
            //how to check if image is null or not setup then show default image?

        }
...
}

Please help, thanks all.

Upvotes: 1

Views: 850

Answers (1)

snrlx
snrlx

Reputation: 5107

I did some further research on that topic. I inspected the Phonegap code (unfortunately I only had version 2.8 right here.

And I found this:

  private JSONObject photoQuery(Cursor cursor, String contactId) {
           JSONObject photo = new JSONObject();
           try {
               photo.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo._ID)));
               photo.put("pref", false);
               photo.put("type", "url");
               Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId)));
               Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
               photo.put("value", photoUri.toString());
           } catch (JSONException e) {
               Log.e(LOG_TAG, e.getMessage(), e);
           }

           return photo; }

If I interpret this correctly then the URI is just put together generically without even checking whether there is an avatar available. So what you can do is to write your own Phonegap-plugin to build your own native query. Otherwise I belive, there is no way for you to achieve what you are planning to do.

Upvotes: 2

Related Questions