ErGo_404
ErGo_404

Reputation: 1821

Insert contact into Android phonebook

I am trying to insert contacts into the Android contact database. I found a few tutorials (which I did not fully understand), and I've basically copied the methods from the "ContactManager" sample code from the Android SDK.

I use this method to add a new raw contact :

public void createContact(String name, String phone, String mobile, String email) {
ContentResolver cr = mContext.getContentResolver();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mAccount.type)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mAccount.name).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.DATA10, phone)
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.TYPE_WORK).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.DATA11, mobile)
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE).build());
ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Groups.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, 
                ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
                    ContactsContract.CommonDataKinds.Email.TYPE_WORK).build());

As you can see I am trying to insert a name (I will later insert the separate given name and surname), office number and mobile phone number and email.

When I run the code, I obtain this error :

Error inserting dirty=1 mimetype=vnd.android.cursor.item/email_v2 data2=2 [email protected] account_type=vnd.sec.contact.phone account_name=vnd.sec.contact.phone raw_contact_id=2 android.database.sqlite.SQLiteException: table groups has no column named mimetype: , while compiling: INSERT INTO groups(dirty, mimetype, data2, data1, account_type, account_name, raw_contact_id) VALUES(?, ?, ?, ?, ?, ?, ?);

It seems like it is trying to insert some data into the groups database although I don't really understand why.

When I run the ContactManager sample app, it works fine, except that the contacts I add using this app do not appear in the Android phonebook, they are tagged as Invisible for some reason.

Has anyone got a clue of what is going on ?

Thanks !

Upvotes: 2

Views: 1586

Answers (1)

Alok Kulkarni
Alok Kulkarni

Reputation: 2153

You are getting that error because you are trying to insert Email address in Groups table. (The URI should be ContactsContract.Data.CONTENT_URI and not ContactsContract.Groups.CONTENT_URI )

Upvotes: 4

Related Questions