12

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 ............

Community
  • 1
  • 1
KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33
  • I dont want to use **android.provider.Contacts** i want to create a contact using **android.provider.ContactContracts** – KK_07k11A0585 Dec 09 '11 at 11:54
  • 4
    good luck http://stackoverflow.com/questions/4744187/how-to-add-new-contacts-in-android – lnu Dec 09 '11 at 12:02
  • which SDK version are you using? – Lelouch Lamperouge Dec 16 '11 at 00:04
  • Take a look here http://stackoverflow.com/a/4242649/95313 and here http://stackoverflow.com/a/4463797/95313. Both approaches should work. As you can see several different operations are required - adding RawContact itself, adding phone for it etc. To better understand this all you need to read this http://developer.android.com/resources/articles/contacts.html. – Fedor Dec 11 '11 at 00:50
  • Use the [contactslib](http://code.google.com/p/contactslib/wiki/PageName) Library. (Fair warning : it's not being maintained anymore.) [Here's](http://code.google.com/p/contactslib/wiki/PageName) the page where they show you how to add new contacts. This is by far the cleanest method. – st0le Dec 09 '11 at 12:04
  • thank u for ur answer but can u tell me how do i implement PersonContact ContactsHelper and other classes I dont get what this actually does there they do not mentioned anything about **android.provider.ContactContracts** which is the base uri for all contacts – KK_07k11A0585 Dec 09 '11 at 12:14
  • @KK_07k11A0585, It's OpenSource. Do a SVN Checkout (at the page) or you can browse the source code online :) – st0le Dec 09 '11 at 12:47

1 Answers1

6
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

Abhinava
  • 1,030
  • 9
  • 19