0

I am trying to add new contacts from within in my android application. However - when I do it and check my contact list the new contact isn't there? I have ensured the display name is unique and looked at a few threads like How to add new contacts in android but have still had no luck. Any suggestions? I call the contact class from another activity.

public class Contact {

    public Contact(String displayName, String emailAddress, Context context) {
        super();

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();

        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, null)
                .withValue(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.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailAddress)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                        ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                .build());
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID,
                        rawContactInsertIndex)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.DISPLAY_NAME, displayName)
                .build());
        try {
            ContentProviderResult[] res = context.getApplicationContext()
                    .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();
        }
    }
}
Community
  • 1
  • 1
adrian
  • 2,786
  • 2
  • 18
  • 33
  • What's the error you're seeing? – Novae Oct 27 '14 at 13:31
  • No error - in fact I put a toast in the try {} was successful and it displayed. Nothing popping up form the catch at all. – adrian Oct 27 '14 at 13:32
  • Do you see anything in LogCat? Have you set the permissions in the manifest file? – Novae Oct 27 '14 at 13:37
  • Yep - changed manifest and fixed up calling activity and it is working now. Not sure what got it working just happy it is! Thanks – adrian Oct 27 '14 at 13:42

0 Answers0