2

I'm having Facebook for Android on my phone which automatically synces the FB profile pictures of the people in my contact list to my phone.

I'd like to use those pictures within my app where I access ContactsContract.PhoneLookup.

Do I really need the Facebook SDK to do that? I guess not, but I cannot find any evidence of the pictures being saved somewhere around ContactsContract

Janusz
  • 187,060
  • 113
  • 301
  • 369
Dennis Winter
  • 2,027
  • 4
  • 32
  • 45

2 Answers2

4

You simply need to query for the Photo URI and use the URI as per your needs.

This method will help you

/**
 * @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);
}

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

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.

This will help you also (if Uri fails it sets a default placeholder image):

and calling that function (contactimage is an ImageView):

Uri contactphoto = objContact.getPhotoUri();
contactimage.setImageURI(contactphoto);
try {
    String nullString = contactimage.getDrawable().toString();
} catch (java.lang.NullPointerException ex) {
    contactimage.setImageResource(R.drawable.contactplaceholder);
}
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • Wow, that was fast! Thanks so much! – Dennis Winter Sep 14 '10 at 16:27
  • This doesn't work for me, if the contact is a FB-synced-only contact. My only solution is to use the FB graph api, see http://stackoverflow.com/questions/3845570/get-contacts-photo-which-are-synced-with-facebook-for-android/4506152#4506152 – Mathias Conradt Dec 22 '10 at 04:27
  • I removed the "answered"-mark from this answer, as I found out in the meantime that I just had the same pictures in my phone already that some of my contacts used on facebook. :( So you actually ask your users for their facebook credentials? – Dennis Winter Feb 16 '11 at 08:24
  • This gets all photos not just facebook ones? – Donal Rafferty Nov 15 '11 at 15:18
0

This will get you your facebook photos by phoneNumber:

public Bitmap getFacebookPhoto(String phoneNumber) {
    Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Uri photoUri = null;
    ContentResolver cr = this.getContentResolver();
    Cursor contact = cr.query(phoneUri,
            new String[] { ContactsContract.Contacts._ID }, null, null, null);

    if (contact.moveToFirst()) {
        long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID));
        photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);

    }
    else {
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image);
        return defaultPhoto;
    }
    if (photoUri != null) {
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                cr, photoUri);
        if (input != null) {
            return BitmapFactory.decodeStream(input);
        }
    } else {
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image);
        return defaultPhoto;
    }
    Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image);
    return defaultPhoto;
}
Gaff
  • 5,507
  • 3
  • 38
  • 49