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
Flutter Does Not Find Android Sdk
How to Programmatically Enable/Disable Accessibility Service in Android
How to Get the Position Selected in a Recyclerview
Run Code Only Once After an Application Is Installed on Android Device
Mobile Vision API - Concatenate New Detector Object to Continue Frame Processing
Firebase Admin Sdk for Android, Methods Not Found
Android Textview:"Do Not Concatenate Text Displayed with Settext"
How to Show Shadow Around the Linearlayout in Android
Android Studio: Cannot Recover Key
Listen Outgoing Sms or Sent Box in Android
Android: How Do Bluetooth Uuids Work
Upload an Image from Camera or Gallery in Webview
Viewpager Detect When User Is Trying to Swipe Out of Bounds
Popupwindow $Badtokenexception: Unable to Add Window -- Token Null Is Not Valid
Gcm Service_Not_Available on Android 2.2
Android:How to Capture a Image of Gps Location