I\'m trying to update a contact of my phone book directly from my app. I\'m able to add and delete the contacts but the update just does nothing!
After the insert or
Finally, I found how to update a contact, here is the code of the update method:
public void update()
{
int id = 1;
String firstname = "Contact's first name";
String lastname = "Last name";
String number = "000 000 000";
String photo_uri = "android.resource://com.my.package/drawable/default_photo";
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// Name
Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastname);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstname);
ops.add(builder.build());
// Number
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?"+ " AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
ops.add(builder.build());
// Picture
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(photo_uri));
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
// Update
try
{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
}
The field id
is the raw contact id returned when you insert a new contact into the database. Here is the code to get this id:
ContentProviderResult[] res;
try
{
res = KramerApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
if (res != null && res[0] != null)
{
String uri = res[0].uri.getPath().substring(14);
r.setBook_id( new Integer(uri).intValue() );
}
}
catch (Exception e)
{
e.printStackTrace();
}
Check my first question if you want to know more about how to insert/delete a contact.
For Updating contact you need to have both contactId and RawContact id... so while updating add this also as a value in where clause/selection args..
something like this..
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
//------------------------------------------------------ Names
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, data.getTitle())
.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, data.getSuffix())
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, data.getFirstName())
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, data.getMiddleName())
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, data.getSuffix())
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, data.getFirstName()+" "+data.getMiddleName()).build());
I am not sure if this helps you, however I found a blog about the difference between contact_id and raw_contact_id:
http://android-contact-id-vs-raw-contact-id.blogspot.de/
In this blog he explains well how the contacts api works and if i understand it correctly you have to use the raw_contact_id to change your entry. So forget the contact_id for update and change your update so that the raw_contact_id entry is changed. I would roughly guess it would look something like this :
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(RawContacts._ID + "=?", new String[]{entertheraw_contact_id})
.withValue(StructuredName.DISPLAY_NAME, "Lost Symbol Characters")
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
I hope this helps you a bit and thank you again for your help form before.