Getting a Photo from a Contact

Getting a Photo from a Contact

Probably this will help you(contact is identified by getId()):

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

Usage is:

Uri u = objItem.getPhotoUri();
if (u != null) {
mPhotoView.setImageURI(u);
} else {
mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}

Android - Get Contact Photo from phone number

public static Bitmap retrieveContactPhoto(Context context, String number) {
ContentResolver contentResolver = context.getContentResolver();
String contactId = null;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};

Cursor cursor =
contentResolver.query(
uri,
projection,
null,
null,
null);

if (cursor != null) {
while (cursor.moveToNext()) {
contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
}
cursor.close();
}

Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
R.drawable.default_image);

try {
if(contactId != null) {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactId)));

if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}

assert inputStream != null;
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return photo;
}

Get image from Contact Swift

There are three properties related to contact images according to the API reference doc from apple:

Image Properties

var imageData: Data? The profile picture of a contact.

var thumbnailImageData: Data? The thumbnail version of the contact’s profile picture.

var imageDataAvailable: Bool Indicates whether a contact has a profile picture.

You can get the CNContact instance from CNContactProperty, and then access imageData in CNContact class.

So your code may look like this:

func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
let contact = contactProperty.contact
if contact.imageDataAvailable {
// there is an image for this contact
let image = UIImage(data: contact.imageData)
// Do what ever you want with the contact image below
...
}
}

How to get photo of the contact in Android?

Instead of ContactsContract.CommonDataKinds.Phone._ID, you must use ContactsContract.CommonDataKinds.Phone.CONTACT_ID to save the cursorID of Contacts table. It solved my problem.

Getting contact photo

I got it, I did a query against the RAW_CONTACT_ID with the MIMETYPE and that gave me the photo I was looking for

Cursor p = context.getContentResolver().query(Data.CONTENT_URI,new String[] {Photo.PHOTO},
Data.RAW_CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE+"'"
,null,null);


Related Topics



Leave a reply



Submit