Insert contact in Android with ContactsContract
Asked Answered
O

4

16

I am trying to add a new contact to the Android 2.2 contacts directly.

//this code doesn't work    
ContentValues cv=new ContentValues();
cv.put(ContactsContract.Contacts.DISPLAY_NAME, "TESTEST");
Uri u= getContentResolver().insert(ContactsContract.Contacts.CONTENT_URI, cv);

gives me the error "Aggregate contacts are created automatically." What am I doing wrong?

This calls the Android's add contact form:

//this code works but it's not ideal
Intent i = new Intent(Intent.ACTION_INSERT_OR_EDIT);
i.setType(Contacts.CONTENT_ITEM_TYPE);
i.putExtra(Insert.NAME, "TESTTEST");
i.putExtra(Insert.PHONE, "209384");
startActivity(i);

I can't figure out how to send it a first name and a last name - only a 'name' which it puts in the first name box. Also I'd like to separate the postal code into street, city, state, and zip which right now is all being put into the street box.

Operational answered 16/12, 2010 at 9:21 Comment(0)
O
27

Add Contact Details in android

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts.Data;
import android.provider.ContactsContract.RawContacts;

public class DemoAddAddressBook extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  /*
   * Uri newPerson = addContactName();
   *
   * addMobilePhoneNo(newPerson); addEmail(newPerson);
   * addPostalAddress(newPerson); addOrganization(newPerson);
   */

  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  int rawContactInsertIndex = ops.size();

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

  //Phone Number
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
    .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
    .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
    .withValue(Phone.TYPE, "1").build());

  //Display name/Contact name
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
    .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
    .build());
  //Email details
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Email.DATA, "[email protected]")
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
    .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "2").build());


  //Postal Address

    ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
        rawContactInsertIndex)
      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POBOX, "Postbox")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, "street")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, "city")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, "region")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, "postcode")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, "country")

      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, "3")


      .build());


  //Organization details
  ops.add(ContentProviderOperation
    .newInsert(ContactsContract.Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID,
      rawContactInsertIndex)
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
    .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, "Devindia")
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
    .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, "Developer")
    .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE )
    .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, "0")

    .build());
  //IM details
    ops.add(ContentProviderOperation
      .newInsert(ContactsContract.Data.CONTENT_URI)
      .withValueBackReference(Data.RAW_CONTACT_ID,
        rawContactInsertIndex)
      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
      .withValue(ContactsContract.CommonDataKinds.Im.DATA, "ImName")
      .withValue(Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE )
      .withValue(ContactsContract.CommonDataKinds.Im.DATA5, "2")


      .build());
  try {
   ContentProviderResult[] res = getContentResolver().applyBatch(
     ContactsContract.AUTHORITY, ops);
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (OperationApplicationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}
Onomatopoeia answered 24/9, 2013 at 2:27 Comment(2)
This will work with multiple phones/emails/... , too, right? Also, where did you get it all from? Are there more fields that can be used, that aren't mentioned here (answer was 4 years ago, so I guess things might have changed) ?Strut
Just want to make a remark here, you MUST add this. java ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE, null) .withValue(RawContacts.ACCOUNT_NAME, null).build()); Before you insert any other details, I was banging my head on the wall because of this tiny misstep.Quiz
S
2

This answer seems to be helpful: https://mcmap.net/q/403174/-how-can-i-launch-the-39-add-contact-39-activity-in-android

It describes how to invoke the contacts entry form using the ContractsContract APIs. These APIs ought to also let you make use of ContactsContract.CommonDataKinds.StructuredPostal to specify the extra fields you want.

Semasiology answered 9/3, 2012 at 11:43 Comment(0)
G
2
    ContentValues p=new ContentValues();
    p.put(RawContacts.ACCOUNT_TYPE, "com.google");
    p.put(RawContacts.ACCOUNT_NAME, "email");
    Uri rowcontect= getContentResolver().insert(RawContacts.CONTENT_URI, p);
    long rawcontectid=ContentUris.parseId(rowcontect);

    ContentValues value = new ContentValues();
    value.put(Data.RAW_CONTACT_ID,rawcontectid);
    value.put(android.provider.ContactsContract.Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE);
    value.put(StructuredName.DISPLAY_NAME, "kunja gajjar");
    getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, value);

    //adding the contents to the data
    ContentValues ppv=new ContentValues();
    ppv.put(android.provider.ContactsContract.Data.RAW_CONTACT_ID, rawcontectid);
    ppv.put(android.provider.ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    ppv.put(Phone.NUMBER, "975657789");
    ppv.put(Phone.TYPE, Phone.TYPE_MOBILE);
    this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, ppv);
Greeley answered 12/3, 2014 at 6:5 Comment(0)
A
1
ContentValues cv = new ContentValues();
cv.put(People.NAME, e1.getText().toString());

// e1.getText().tostring() is Contact name 

Uri u =    getContentResolver().insert(People.CONTENT_URI, cv);

Uri pathu = Uri.withAppendedPath(u, People.Phones.CONTENT_DIRECTORY);    

cv.clear();

cv.put(People.NUMBER, e2.getText().toString());

// e2.getText().tostring() is Contact number

getContentResolver().insert(pathu, cv);

Toast.makeText(getApplicationContext(), "Contact Added",Toast.LENGTH_LONG).show();
Ardie answered 25/10, 2015 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.