Android - Update a contact in the phone book
<p>I'm still working on my phone book app and I want to update a contact that is stored into the stock phone book. I'm able to insert a new contact but update has no effect at all.</p>

<p>Here is the code I use:</p>

<pre><code>public void update(Relation r)
{
    ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;();

    // Name
    Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, r.getBook_id());
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName()+ " " +r.getLastName());
    ops.add(builder.build());

    // Number
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, r.getBook_id());
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
    builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    ops.add(builder.build());

    // Picture
    try
    {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(MyApplication.getInstance().getContentResolver(), Uri.parse(r.getPhoto()));
        ByteArrayOutputStream image = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, r.getBook_id());
        builder.withValue(ContactsContract.Data.MIMETYPE,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
    {
        MyApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
</code></pre>

<p>The one above doesn't work but this one does (Insert):</p>

<pre><code>public void insert(Relation r)
{
    ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;();
    int rawContactInsertIndex = ops.size();

    Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_TYPE, null);
    builder.withValue(RawContacts.ACCOUNT_NAME, null);
    ops.add(builder.build());

    // Name
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName()+ " " +r.getLastName());
    ops.add(builder.build());

    // Number
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
    builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    ops.add(builder.build());

    // Picture
    try
    {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(MyApplication.getInstance().getContentResolver(), Uri.parse(r.getPhoto()));
        ByteArrayOutputStream image = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
        builder.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
        ops.add(builder.build());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    // Add the new contact
    ContentProviderResult[] res;
    try
    {
        res = MyApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        if (res != null &amp;&amp; res[0] != null)
        {
            String uri = res[0].uri.getPath().substring(14);
            r.setBook_id(new Integer(uri).intValue());
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
</code></pre>

<p>Can you tell me if I'm doing it in the good way or not?</p>

<p>Thanks.</p>

<p>Regards.</p>

<p>V.</p>
