sai
sai

Reputation: 2562

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);

Upvotes: 10

Views: 15658

Answers (3)

Bruno Davi Delta
Bruno Davi Delta

Reputation: 1

I know it's already been resolved, but I had trouble adapting the code I was working on, so here's another more 'basic' alternative

public void addContact(View view)
    {
        String name = editName.getText().toString();
        String number = editNumber.getText().toString();

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

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

        ops.add(
                ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
                    )
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
                    .build()
            );

        ops.add(
            ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
                )
                .withValue(
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    number
                )
                .withValue(
                    ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
                )
                .build()
            );

        try
        {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        }
        catch (OperationApplicationException e)
        {}
        catch (RemoteException e)
        {}
    }

now the imports I had to use:

// To Android App
import android.app.*;
import android.os.*;
import java.util.*;
import android.view.*;
import android.widget.*;

// Only Add Contact
import android.content.*;
import android.provider.*;

Upvotes: 0

via ferrata
via ferrata

Reputation: 57

other option....

public void insert() {
    Intent intent = new Intent(
            ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
            ContactsContract.Contacts.CONTENT_URI);
    intent.setData(Uri.parse("tel:911"));//specify your number here
    intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Emergency USA");
    startActivity(intent);
    Toast.makeText(this, "Record inserted", Toast.LENGTH_SHORT).show();
}

Upvotes: 3

bindal
bindal

Reputation: 1932

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"/>

Upvotes: 11

Related Questions