Reputation: 27
I am using the code below to access the phone numbers and the names of the contact. I want to add to this function a way to also add the photo of the contact.
void loadContacts() {
ContentResolver contentResolver=getContentResolver();
Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
if(cursor.getCount() > 0) {
while (cursor.moveToNext()) {String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor cursor2 = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (cursor2.moveToNext()) {
String phoneNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
make_contact(name,phoneNumber);
}
cursor2.close();
}
}
}
cursor.close();
}
Upvotes: 1
Views: 103
Reputation: 3319
For Bitmap Image,
private Bitmap retrieveContactPhoto(String contactID) {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
}
assert inputStream != null;
inputStream.close();
return photo ;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
By using contact Id you can retrieve bitmap image and set to imageview.
Upvotes: 1