Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You simply need to query for the Photo URI and use the URI as per your needs.</p> <p>This method will help you</p> <pre><code>/** * @return the photo URI */ 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; // no photo } } else { return null; // error in cursor process } } catch (Exception e) { e.printStackTrace(); return null; } Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long .parseLong(getId())); return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); } </code></pre> <p>Or there is another approach:</p> <pre><code>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; } </code></pre> <p>Please note that the URI record may not exists. If the images for example are stored on the SDCard and that is not present, the URI will return no photo about it. For this you need to do further checks.</p> <p>This will help you also (if Uri fails it sets a default placeholder image):</p> <p>and calling that function (contactimage is an ImageView):</p> <pre><code>Uri contactphoto = objContact.getPhotoUri(); contactimage.setImageURI(contactphoto); try { String nullString = contactimage.getDrawable().toString(); } catch (java.lang.NullPointerException ex) { contactimage.setImageResource(R.drawable.contactplaceholder); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload