Thursday, January 26, 2012

[android-developers] Changing contact photo problem

i'm trying to change contact's photo by using this code:

public void setContactPhoto(byte[] bytes, long personId) {

ContentValues values = new ContentValues();

int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " = " +
personId
+ " AND " + ContactsContract.Data.MIMETYPE + "=='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null,
where, null, null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if (cursor.moveToFirst()) {
photoRow = cursor.getInt(idIdx);
}
cursor.close();

values.put(ContactsContract.Data.RAW_CONTACT_ID, personId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
try {
if (photoRow >= 0) {
cr.update(ContactsContract.Data.CONTENT_URI, values,
ContactsContract.Data._ID + " = " + photoRow, null);


} else {
cr.insert(ContactsContract.Data.CONTENT_URI, values);

}
} catch (Exception e) {

}

}


For most contacts it's work fine, but for other I have a problem.
When I tried to debug it, I noticed for some things:

1.For some contacts the query return -1 (when for other it's return
photorow>0 even I'm not set their photo before )

2. When the query return -1 and I'm using to insert new row I cant see
the photo in the contacts list

3. Sometimes I get photoRow>0 but not belong to the contact I tried to
update it's photo.

4.Sometimes I get more than one result so I can't know which one of
them I need in order to update the contact photo/

can anybody help me how to deal with this problem?
I thought that the query should return me a specific row which belong
to a specific contact.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate