0

I'm using ContactsContract to save a lot of data to contacts (I didn't really expect the second .apply{} to work, but hey, I tried). When the activity is launched, the only fields that contain the info i put in them are: First Name, Middle Name, Last Name, Company, Title, Phone1-3 + their labels , email1 and 2 + their labels, and Notes.

This means i'm missing Prefix, Suffix, Nickname, IM handle and type, the entirety of home and work addresses, and both dates (birtdhay and anniversary). Of course, I frist tried with only the first .apply{}, but the result is the same. What am I doing wrong?

val intent = Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI).apply {
            type = ContactsContract.RawContacts.CONTENT_TYPE
            putExtra(ContactsContract.Intents.Insert.NAME, fullName)
            putExtra(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
            putExtra(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
            putExtra(ContactsContract.CommonDataKinds.StructuredName.PREFIX, prefix)
            putExtra(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix)
            putExtra(ContactsContract.CommonDataKinds.Nickname.NAME, nickname)

            putExtra(ContactsContract.Intents.Insert.EMAIL, homeEmail)
            putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME)
            putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL, workEmail)
            putExtra(ContactsContract.Intents.Insert.SECONDARY_EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)

            putExtra(ContactsContract.Intents.Insert.PHONE, cellPhone)
            putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
            putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, homePhone)
            putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
            putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE, workPhone)
            putExtra(ContactsContract.Intents.Insert.TERTIARY_PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)

            putExtra(ContactsContract.Intents.Insert.IM_HANDLE, imHandle)
            putExtra(ContactsContract.Intents.Insert.IM_PROTOCOL, imProtocol)

            putExtra(ContactsContract.Intents.Insert.COMPANY, organization)
            putExtra(ContactsContract.Intents.Insert.JOB_TITLE, jobTitle)
            putExtra(ContactsContract.CommonDataKinds.Website.URL, website)
            putExtra(ContactsContract.Intents.Insert.NOTES, notes)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.STREET, homeAddressStreetName)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.CITY, homeLocality)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.REGION, homeRegion)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, homePostalCode)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, homeCountry)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME)

            putExtra(ContactsContract.CommonDataKinds.Event.START_DATE, birthday)
            putExtra(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
        }.apply { 
            type = ContactsContract.RawContacts.CONTENT_TYPE
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.STREET, workAddressStreetName)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.CITY, workLocality)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.REGION, workRegion)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, workPostalCode)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, workCountry)
            putExtra(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
            
            putExtra(ContactsContract.CommonDataKinds.Event.START_DATE, anniversary)
            putExtra(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY)
        }
        startActivity(intent)

1 Answer 1

0

You can't use constants under ContactsContract.CommonDataKinds.X with Intent.ACTION_INSERT

The list of supported fields is here: https://developer.android.com/reference/android/provider/ContactsContract.Intents.Insert#constants_1

So for example home address might be added like this:

putExtra(ContactsContract.Intents.Insert.POSTAL, "Contacts Lane 123, SomeCity, OM United States");
putExtra(ContactsContract.Intents.Insert.POSTAL_TYPE, StructuredPostal.TYPE_HOME);

But note you can't add multiple postal addresses with this.

The special DATA constant is different, it allows you to add whatever data you want by specifying the value and mimetype:

  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);

  // add more rows here if needed of any type

  intent.putParcelableArrayListExtra(Insert.DATA, data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.