Reputation: 74046
I am having a problem getting a contact photo, the kind you see in the messaging app and new gmail notification. I have looked at a few example codes but nothing has worked for me, this is what I currently have
this should get the photo uri and turn it into a bitmap image to use or at least it seems
public static Bitmap getContactImage(long id,Context context){
InputStream input = getPhoto(id,context);
if(input == null){
return null;
}
return BitmapFactory.decodeStream(input);
}
public static InputStream getPhoto(long contactId,Context context){
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
InputStream in = null;
try{
in = context.getContentResolver().openInputStream(photoUri);
}catch(FileNotFoundException e){
Log.d(TAG, e.toString());
}
return in;
}
and this is how I call
long contactID = 0;
Bitmap image = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_contact_picture);
Cursor contact = context.getContentResolver().query(Data.CONTENT_URI,new String[] {Data.CONTACT_ID},Email.ADDRESS + "='" + from + "'",null,null);
if(contact.moveToFirst() && contact != null){
contactID = contact.getLong(0);
image = getContactImage(contactID,context);
}
I get the contact id fine (checked by searching the number for the person querying against) but then it does not find the contact photo. I know there is a photo because I am testing it against myself to make sure and I have a contact photo so I dont know what else I should do.
I always find navigating the contact provider very troublesome because there is so much to it.
Upvotes: 0
Views: 2788
Reputation: 74046
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);
Upvotes: 1
Reputation: 2153
This works for me.
//Querying for all contacts(Apply selection parameter in query to get a specific contact)
Uri contacts = ContactsContract.Contacts.CONTENT_URI;
cur = null;
cur = Main.context.getContentResolver().query(contacts, null, null,
null, null);
int contactIdIndex = cur.getColumnIndex(ContactsContract.PhoneLookup._ID);
int contactId = cur.getInt(contactIdIndex);
// Photo
Uri contactUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = cr
.query(
photoUri,
new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO },
null, null, null);
if (cursor != null && cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
_conEntry.setPhoto(data);
//Data is the photo bytes for you
}
if (cursor != null)
cursor.close();
Upvotes: 0
Reputation: 2595
You're doing it wrong.
First, get an ID of the photo from the PHOTO_ID column of ContactsContract.Contacts table. Next, retrieve a byte array from PHOTO column (which is actually alias to DATA15) from ContactsContract.Data by ID you got in previous step. And, finally, decode that byte array using BitmapFactory to get a bitmap. Here are docs about this.
Upvotes: 0