Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a contact programmatically [duplicate]

Possible Duplicate:
How to add new contacts in android

public boolean createContact(String name, String number, String email) 
{
        boolean success = true;

        try
        {
            ContentValues contentValues = new ContentValues();

            ContentResolver contentResolver  = getContentResolver();

            contentValues.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
            contentValues.put(Phone.NUMBER, "123254");
            Uri uri = contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, contentValues);

            if(uri==null)
            {
                success = false;
            }

        }
        catch (Exception e) 
        {
            e.printStackTrace();
            success = false;
        }
        return success;
}

I am getting NullPointer Exception I don't know why I have also specified WRITE_CONTACTS Permission. Please help me ............

like image 382
KK_07k11A0585 Avatar asked Dec 07 '11 10:12

KK_07k11A0585


1 Answers

ContentValues values = new ContentValues();
            values.put(Data.RAW_CONTACT_ID, 001);
            values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
            values.put(Phone.NUMBER, "            1-800-GOOG-411      ");
            values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
            values.put(Phone.LABEL, "free directory assistance");
            Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

Use the above code. You are not providing id. http://developer.android.com/reference/android/provider/ContactsContract.Data.html

like image 95
Abhinava Avatar answered Oct 07 '22 03:10

Abhinava