AlexPaduraru
AlexPaduraru

Reputation: 31

insert contact using prepopulated intent

I am trying to insert a contact using an intent and I tried exactly this piece of code, taken from Android Sdk, see it here:

ArrayList<ContentValues> data = new ArrayList<ContentValues>();

ContentValues row1 = new ContentValues();
row1.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
row1.put(Organization.COMPANY, "Android");
data.add(row1);

ContentValues row2 = new ContentValues();
row2.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
row2.put(Email.TYPE, Email.TYPE_CUSTOM);
row2.put(Email.LABEL, "Green Bot");
row2.put(Email.ADDRESS, "[email protected]");
data.add(row2);

Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
intent.putParcelableArrayListExtra(Insert.DATA, data);

startActivity(intent);

And I get an empty InsertContact intent. It drives me crazy, why is it not working?

Upvotes: 2

Views: 784

Answers (2)

ahmed hamdy
ahmed hamdy

Reputation: 5169

Maybe you your Implementation for Data , Organization and Email Class is wrong.

This is the same code But with full implementation of Classes :

ArrayList<ContentValues> data = new ArrayList<ContentValues>();

ContentValues row1 = new ContentValues();
     row1.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
     row1.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "Android");
data.add(row1);

ContentValues row2 = new ContentValues();
    row2.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
    row2.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM);
    row2.put(ContactsContract.CommonDataKinds.Email.LABEL, "Green Bot");
    row2.put(ContactsContract.CommonDataKinds.Email.ADDRESS, "[email protected]");
data.add(row2);

Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
    intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);    
startActivity(intent); 

Note: this code works only for API Level 11 and above AS ContactsContract.Intents.Insert.DATA from API Level 11 Android Doc

Upvotes: 0

Givi
Givi

Reputation: 3283

Try this:

Intent addPersonIntent = new Intent(Intent.ACTION_INSERT);
                                        addPersonIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);

                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.NAME, "name");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.PHONE, "phone");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, "email");
                                        addPersonIntent.putExtra(ContactsContract.Intents.Insert.POSTAL, "address");

                                        startActivityForResult(addPersonIntent, CREATE_NEW);

Upvotes: 2

Related Questions