You just need to request a photo URI and use the URI to suit your needs.
This method will help you.
public Uri getPhotoUri() { try { Cursor cur = this.ctx.getContentResolver().query( ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null, null); if (cur != null) { if (!cur.moveToFirst()) { return null;
Or there is another approach:
public Uri getPhotoUri(Integer contactid) { Cursor photoCur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'", null, ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC"); photoCur.moveToPosition(contactid); Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, photoCur.getLong(photoCur.getColumnIndex(ContactsContract.Contacts._ID))); Uri photo = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); return photo; }
Note that the URI entry may not exist. If the images, for example, are stored on the SD card and they are not there, the URI will not return photos about it. To do this, you need to perform additional checks.
It will also help you (if Uri fails, it will set the default placeholder image):
and calling this function (contactimage - ImageView):
Uri contactphoto = objContact.getPhotoUri(); contactimage.setImageURI(contactphoto); try { String nullString = contactimage.getDrawable().toString(); } catch (java.lang.NullPointerException ex) { contactimage.setImageResource(R.drawable.contactplaceholder); }
source share