androidcontactsandroid-contactscontactscontractrawcontacts

Insert RawContact with a specific contactid


I am trying to create a rawcontact in android that has a specific contact id, so it is linked to other rawcontacts with the same contactid (not rawcontactid).

The problem is I am unable to insert the Contact_ID into the ContentProviderOpertations. Using the following code return "Insert failed"

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int id = (int) contactId;

    String condition = Data.RAW_CONTACT_ID + "=?";
    String[] parameters = { "" + id };

    try {

        String accountName = account.name;
        String accountType = account.type;
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, accountType)
                .withValue(RawContacts.ACCOUNT_NAME, accountName).build());
        ops.add(getAccountGroupOperation(account));

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.CONTACT_ID, "" + id)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, contact.getName())
                .build()); 
        MyApplication.getAppContext().getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
        ...
    }

What am I doing wrong?


Solution

  • It is possible to do this but not in the way you want. You can make Contacts application join two raw contacts by adding both raw contact ids to AggregationExceptions table by setting the type as TYPE_KEEP_TOGETHER

    http://developer.android.com/reference/android/provider/ContactsContract.AggregationExceptions.html