Cannot insert android contacts programmatically into android device
Asked Answered
F

2

8
ArrayList<ContentProviderOperation> ops =new ArrayList<ContentProviderOperation>();
         ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID, 3)
                  .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                  .withValue(Phone.NUMBER, "999999999")
                  .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                  .withValue(Phone.DISPLAY_NAME, "hhhhhhh")
                  .withValue(ContactsContract.CommonDataKinds.Email.DATA, "[email protected]")
                  .build());
         ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID, 4)
                  .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                  .withValue(Phone.NUMBER, "999999999")
                  .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                  .withValue(Phone.DISPLAY_NAME, "hhhlllllllllll")
                  .withValue(ContactsContract.CommonDataKinds.Email.DATA, "[email protected]")
                  .build());

         try {
             cr.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I have added <uses-permission android:name="android.permission.WRITE_CONTACTS"/> permission.

I am getting the following errors:

09-25 09:30:41.365: W/System.err(1057): android.content.OperationApplicationException: insert failed
09-25 09:30:41.375: W/System.err(1057):     at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:161)
09-25 09:30:41.375: W/System.err(1057):     at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:461)
09-25 09:30:41.375: W/System.err(1057):     at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:225)
09-25 09:30:41.375: W/System.err(1057):     at android.content.ContentResolver.applyBatch(ContentResolver.java:901)
09-25 09:30:41.375: W/System.err(1057):     at com.example.projdemo.MainActivity.onCreate(MainActivity.java:136)
09-25 09:30:41.375: W/System.err(1057):     at android.app.Activity.performCreate(Activity.java:5008)
09-25 09:30:41.385: W/System.err(1057):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-25 09:30:41.395: W/System.err(1057):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 09:30:41.395: W/System.err(1057):     at android.os.Looper.loop(Looper.java:137)
09-25 09:30:41.395: W/System.err(1057):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-25 09:30:41.395: W/System.err(1057):     at java.lang.reflect.Method.invokeNative(Native Method)
09-25 09:30:41.395: W/System.err(1057):     at java.lang.reflect.Method.invoke(Method.java:511)
09-25 09:30:41.395: W/System.err(1057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-25 09:30:41.395: W/System.err(1057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 09:30:41.405: W/System.err(1057):     at dalvik.system.NativeStart.main(Native Method)
Fonsie answered 25/9, 2012 at 4:23 Comment(0)
A
40

Try as follows :

private void addContact() {
    ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>(); 
    op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 
            //.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT) 
            .build()); 

    // first and last names 
    op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
            .withValue(StructuredName.GIVEN_NAME, "Second Name") 
            .withValue(StructuredName.FAMILY_NAME, "First Name") 
            .build()); 

    op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
            .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "09876543210")
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_HOME)
            .build());
    op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)

            .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, "[email protected]")
            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, Email.TYPE_WORK)
            .build());

    try{ 
        ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list); 
    }catch(Exception e){ 
        e.printStackTrace(); 
    } 
}
Arsenious answered 25/9, 2012 at 4:37 Comment(7)
@Pied Piper, you have written .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null), can you please kindly elaborate use of this 2. I wan to insert contact as Local Contact not any gmail contact.Zackzackariah
GIVEN_NAME would actually correspond to first name and FAMILY_NAME to last name. You inverted the 2 values.Sikorsky
@Sikorsky AFAIK : It is based on a country/state/religion. What's in a name?! :)Arsenious
Keep AGGREGATION_MODE line commented. It solved my issue with tab2. Well, what was happening in my case is the contact was not being added in tab2. Besides this app was working fine in other devices.Gallego
for add image wptrafficanalyzer.in/blog/…Atory
Yes this does in fact work where doing everything in a single add command does not. However, I'm not entirely sure why that is the case.Lionel
In order to link google account replace with this : .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "com.google") .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "youremailaddress")Cancellation
S
1

Hi I know its bit late but it may help you. First of all you must register the uses permission in manifest file as follows:

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

then you can try with the following method:

private void addContact(String name, String phone) {

        ContentValues values = new ContentValues();
        values.put(Contacts.People.NUMBER, phone);
        values.put(Contacts.People.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
        values.put(Contacts.People.LABEL, name);
        values.put(Contacts.People.NAME, name);
        Uri dataUri = getContentResolver().insert(Contacts.People.CONTENT_URI, values);
        Uri updateUri = Uri.withAppendedPath(dataUri, Contacts.People.Phones.CONTENT_DIRECTORY);
        values.clear();
        values.put(Contacts.People.Phones.TYPE, Contacts.People.TYPE_MOBILE);
        values.put(Contacts.People.NUMBER, phone);
        updateUri = getContentResolver().insert(updateUri, values);
        Log.d("CONTACT", ""+updateUri);
    }
Semicircle answered 27/1, 2016 at 8:23 Comment(1)
Works! awesome.Venegas

© 2022 - 2024 — McMap. All rights reserved.