title: Writing contact to contact database - Android 2.1
body: <p>I'm trying to write a new contact to the contact database in Android 2.1. But I am getting the following error:</p>
  
  <pre><code>java.lang.UnsupportedOperationException: Aggregate contacts are created automatically.
  </code></pre>
  
  <p>I have added the following fields in the manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_CONTACTS" /&gt;
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;();
  
  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);
  }
  </code></pre>