Writing a contact in the contact database - Android 2.1

I am trying to write a new contact with a contact database in Android 2.1. But I get the following error:

java.lang.UnsupportedOperationException: Aggregate contacts are created automatically.

I added the following fields to the manifest:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

This is my code:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

String accountType = null;
String accountName = null;

Builder builder = ContentProviderOperation.newInsert(
ContactsContract.Contacts.CONTENT_URI);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName);

ContentProviderOperation op = builder.build();
Log.i("contacts", "op.getUri(): " + op.getUri());
ops.add(op);

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(
        ContactsContract.Data.RAW_CONTACT_ID,
        ops.size()
    )
    .withValue(
        ContactsContract.Data.MIMETYPE,
        StructuredName.CONTENT_ITEM_TYPE
    )
    .withValue(StructuredName.DISPLAY_NAME, contact.getFullName())
    .build()
);

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(
        ContactsContract.Data.RAW_CONTACT_ID,
        ops.size()
    )
    .withValue(
        ContactsContract.Data.MIMETYPE,
        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
    )
    .withValue(
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        contact.getPhoneNumber()
    )
    .build()
);

try {
    this.cr.applyBatch(ContactsContract.AUTHORITY, ops);
}
+3
source share
1 answer
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rowContactInsertIndex).withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE).withValue(Phone.NUMBER, cPhone).withValue(Phone.TYPE, cPhoneType).build());

it can help you. do the same for full name, phone and email too

+2
source

Source: https://habr.com/ru/post/1754201/


All Articles