Created
November 11, 2015 15:33
-
-
Save Jeevuz/767b195b9b70b80dcc75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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