In this week’s project, I encountered the need to add contacts or to add existing contacts. There are many fields that need to be saved in contacts, which I am not familiar with before, so I will summarize them here.
field
Contact Name
The name was set to the Intent and then passed through the Intent itself
ContentValues row1 = new ContentValues(); String name = lastName + middleName + firstName; row1.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); row1.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name); row1.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName); row1.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName); row1.put(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);Copy the code
Contact name
ContentValues row2 = new ContentValues(); row2.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE); row2.put(ContactsContract.CommonDataKinds.Nickname.NAME, nickName);Copy the code
Contact profile picture
Here we need to pass in the byte array of the image
ContentValues row3 = new ContentValues(); / / add avatar row3. Put (ContactsContract. Data. MIMETYPE, ContactsContract.Com monDataKinds. Photo. CONTENT_ITEM_TYPE); Bitmap bitmap = BitmapFactory.decodeFile(photoFilePath); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); row3.put(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray());Copy the code
Contact Remarks
ContentValues row4 = new ContentValues(); row4.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE); row4.put(ContactsContract.CommonDataKinds.Note.NOTE, remark);Copy the code
Contact Number
There are many types of numbers, phone, mobile, fax, company, home, etc
ContentValues row5 = new ContentValues(); / / addPhoneNumber contact telephone number (row5, values, mobilePhoneNumber, ContactsContract.Com monDataKinds. Phone. TYPE_MOBILE); ContentValues row6 = new ContentValues(); / / contact Phone addPhoneNumber (row6, values, hostNumber, ContactsContract.Com monDataKinds. Phone. TYPE_COMPANY_MAIN); ContentValues row7 = new ContentValues(); / / contact number addPhoneNumber (row7, values, workPhoneNumber, ContactsContract.Com monDataKinds. Phone. TYPE_WORK_MOBILE); ContentValues row8 = new ContentValues(); / / contact fax addPhoneNumber (row8, values, workFaxNumber, ContactsContract.Com monDataKinds. Phone. TYPE_FAX_WORK); ContentValues row9 = new ContentValues(); / / contact housing number addPhoneNumber (row9, values, homePhoneNumber, ContactsContract.Com monDataKinds. Phone. TYPE_HOME); ContentValues row10 = new ContentValues(); / / contact residential fax addPhoneNumber (row10, values, homeFaxNumber, ContactsContract.Com monDataKinds. Phone. TYPE_FAX_HOME); Private void addPhoneNumber(ContentValues row, ArrayList<ContentValues> values, String phoneNumber, int type) { row.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); row.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber); row.put(ContactsContract.CommonDataKinds.Phone.TYPE, type); values.add(row); }Copy the code
Contact companies and positions
Row11 = new ContentValues(); row11.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE); row11.put(ContactsContract.CommonDataKinds.Organization.COMPANY, organization); row11.put(ContactsContract.CommonDataKinds.Organization.TITLE, title);Copy the code
Web site
ContentValues row12 = new ContentValues(); row12.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE); row12.put(ContactsContract.CommonDataKinds.Website.URL, url);Copy the code
Contact email
Row13 = new ContentValues(); row13.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); row13.put(ContactsContract.CommonDataKinds.Email.DATA, email); row13.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);Copy the code
Contact Address
Addresses are divided into home, work and others. There is a problem. It cannot be displayed after the subsections are passed in, so you can only splice the country, province and street and pass in the address field, so that it can be displayed, but the postal code cannot be displayed
ContentValues row14 = new ContentValues(); addAddress(row14, values, addressCountry, addressState, addressCity, addressStreet, addressPostalCode, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER); Row15 = new ContentValues(); addAddress(row15, values, homeAddressCountry, homeAddressState, homeAddressCity, homeAddressStreet, homeAddressPostalCode, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME); ContentValues row16 = new ContentValues(); addAddress(row16, values, workAddressCountry, workAddressState, workAddressCity, workAddressStreet, workAddressPostalCode, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK); Private void addAddress(ContentValues row, ArrayList<ContentValues> values, String country, String region, String city, String street, String addressPostalCode, int type) { row.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE); row.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, country + region + city + street); row.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country); row.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, region); row.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city); row.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street); row.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, addressPostalCode); row.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, type); values.add(row); }Copy the code
Add the way
There are three ways to add, one is silent add, directly save to the database, the other two are jump, directly add or add to the existing contact
1. Add silently
Take adding a name as an example and insert it directly into the database
// Insert rawcontacts.content_uri into the null value, // Get the rawContactId returned by the Android system. // Insert Uri rawContactUri = based on this ID mActivity.getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); / / content type values. The put (ContactsContract. Data. MIMETYPE, ContactsContract.Com monDataKinds. StructuredName. CONTENT_ITEM_TYPE); / / contact name values. The put (ContactsContract.Com monDataKinds. StructuredName. GIVEN_NAME, firstName); values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName); values.put(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName); / / to the contact URI add contact name mActivity getContentResolver (), insert (ContactsContract. Data. CONTENT_URI, values).Copy the code
2. Jump to add
Add all of the above rows to the array and pass them together
List<ContentValues> values = new ArrayList<>(); // Add data to be set... Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI); intent.putExtra(ContactsContract.Intents.Insert.NAME, name); intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, values); mActivity.startActivity(intent);Copy the code
2. Add a contact to an existing contact
Add all of the above rows to the array and pass them together
List<ContentValues> values = new ArrayList<>(); // Add data to be set... Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT); intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, values); startActivity(intent);Copy the code
permissions
Do not forget the read and write permissions of the contact. Otherwise, an error will be displayed
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
Copy the code
Summary so far, still calculate more detailed, have additional welcome comment.