Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Start activity for result to pick contact phone
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT_REQUEST_CODE);
// In the onActivityResult
case PICK_CONTACT_REQUEST_CODE:
if (resultCode == RESULT_OK) {
// Get the URI that points to the selected contact
Uri contactDataUri = data.getData();
String[] projection = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
};
// Perform the query on the contact to get the info. Better do this asynchronously using Loaders.
Cursor cursor = getContentResolver()
.query(contactDataUri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int displayNameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int contactIdColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
String number = cursor.getString(numberColumn);
String name = cursor.getString(displayNameColumn);
int contactId = cursor.getInt(contactIdColumn);
// To get the path of the thumbnail
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
Uri thumbnailUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
String imagePath = copyToTemp(thumbnailUri);
// Alternatively you can use ContactsContract.Contacts.openContactPhotoInputStream() to get input stream
// Use the name, number and thumbnail...
} else {
// No cursor or its empty
}
cursor.close();
}
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment