How to use contact photos synced with Facebook for Android

I have Facebook for Android on my phone that automatically syncs FB profile photos with people from my contact list on my phone.

I would like to use these photos in my application, where I refer to ContactsContract.PhoneLookup .

Do I really need a Facebook SDK? Probably not, but I can’t find any evidence that the images are saved somewhere around ContactsContract

+2
source share
2 answers

You just need to request a photo URI and use the URI to suit 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; } 

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); } 
+4
source

This will give you your facebook photos by phone number:

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

Source: https://habr.com/ru/post/948497/


All Articles