Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add contacts programmatically in Android?

I have tried this but Contacts were not added!

ContentResolver cr = this.getContentResolver();
ContentValues cv = new ContentValues();
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "sai1");
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "9640954335");
cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
cr.insert(ContactsContract.RawContacts.CONTENT_URI, cv);
like image 618
sai Avatar asked Feb 01 '12 12:02

sai


1 Answers

Try to use following code

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

        Log.i("Line38", "Here");
           ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)  
                        .withValue(RawContacts.ACCOUNT_TYPE, AccountManager.KEY_ACCOUNT_TYPE)          
                        .withValue(RawContacts.ACCOUNT_NAME, AccountManager.KEY_ACCOUNT_NAME)          
                        .build());

        ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)      
                        .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)      
                        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)      
                        .withValue(StructuredName.DISPLAY_NAME, "u232786seee")
                        .withValue(StructuredName.IN_VISIBLE_GROUP,true)
                        .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"23232343434")
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, "4343")
        .build());

        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, "")
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, "")
        .build());

        //Log.i("Line43", Data.CONTENT_URI.toString()+" - "+rawContactInsertIndex);

        try {
                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();
        }

And add below permission in manifest file

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
like image 117
bindal Avatar answered Sep 19 '22 07:09

bindal