SoFunction
Updated on 2025-04-06

How to add contacts to address book on Android

Fields

Contact name

I don't know why the name is, but after the value is set, I passed it alone again through Intent

// Contact nameContentValues row1 = new ContentValues();String name = lastName + middleName + firstName;(, 
.CONTENT_ITEM_TYPE);(.DISPLAY_NAME, 
name);(.GIVEN_NAME, 
firstName);(.FAMILY_NAME, 
lastName);(.MIDDLE_NAME, 
middleName);

Contact nickname

ContentValues row2 = new ContentValues();
(, .CONTENT_ITEM_TYPE);
(, nickName);

Contact profile picture

Here you need to pass the byte array of the image into

ContentValues row3 = new ContentValues();
//Add avatar(, .CONTENT_ITEM_TYPE);
Bitmap bitmap = (photoFilePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
(, 100, baos);
(, ());

Contact Notes

// Contact NotesContentValues row4 = new ContentValues();
(, .CONTENT_ITEM_TYPE);
(, remark);

Contact number

There are many types of numbers, phone, cell phone, fax, company, home, etc.

ContentValues row5 = new ContentValues();
// Contact numberaddPhoneNumber(row5, values, mobilePhoneNumber, 
.TYPE_MOBILE);ContentValues row6 = new ContentValues();
// Contact person's company phone numberaddPhoneNumber(row6, values, hostNumber, 
.TYPE_COMPANY_MAIN);ContentValues row7 = new ContentValues();
// Contact's work numberaddPhoneNumber(row7, values, workPhoneNumber, 
.TYPE_WORK_MOBILE);ContentValues row8 = new ContentValues();
// Contact's work faxaddPhoneNumber(row8, values, workFaxNumber, 
.TYPE_FAX_WORK);ContentValues row9 = new ContentValues();
// Contact's residential numberaddPhoneNumber(row9, values, homePhoneNumber, 
.TYPE_HOME);ContentValues row10 = new ContentValues();
// Contact's residential faxaddPhoneNumber(row10, values, homeFaxNumber, 
.TYPE_FAX_HOME);

//How to add packageprivate void addPhoneNumber(
    ContentValues row, ArrayList<ContentValues> values, String phoneNumber, int type) {    
    (, 
    .CONTENT_ITEM_TYPE);    
    (, phoneNumber);    
    (, type);    
    (row);
}

Contact company and position

// Contact company and positionContentValues row11 = new ContentValues();
(, .CONTENT_ITEM_TYPE);
(, organization);
(, title);

website

// Contact websiteContentValues row12 = new ContentValues();
(, .CONTENT_ITEM_TYPE);
(, url);

Contact email

// Insert Email dataContentValues row13 = new ContentValues();
(, .CONTENT_ITEM_TYPE);
(, email);
(, .TYPE_WORK);

Contact address

Addresses are divided into home, work and others. There is a problem. After the segment is imported, it cannot be displayed. You can only splice the country, province, city and streets by yourself and pass it into the address field. This can be displayed, but the postal code cannot be displayed.

//Other addressesContentValues row14 = new ContentValues();
addAddress(row14, values, addressCountry, addressState, addressCity, addressStreet, addressPostalCode, .TYPE_OTHER);
//Home addressContentValues row15 = new ContentValues();
addAddress(row15, values, homeAddressCountry, homeAddressState, homeAddressCity, homeAddressStreet, homeAddressPostalCode, .TYPE_HOME);
//Work addressContentValues row16 = new ContentValues();
addAddress(row16, values, workAddressCountry, workAddressState, workAddressCity, workAddressStreet, workAddressPostalCode, .TYPE_WORK);

//Add address methodprivate void addAddress(ContentValues row, ArrayList<ContentValues> values, String country, String region, String city, String street, String addressPostalCode, int type) {
    (, .CONTENT_ITEM_TYPE);
    (.FORMATTED_ADDRESS, country + region + city + street);
    (, country);
    (, region);
    (, city);
    (, street);
    (, addressPostalCode);
    (, type);
    (row);
}

Add method

There are three ways to add, one is to add silently and directly store it in the database, and the other two are to jump, add it directly or add it to existing contacts.

1. Add silently

Take the addition of a name as an example and insert it directly into the database

// Insert into RawContacts.CONTENT_URI null value,// Get the rawContactId returned by the Android system first// The value should be inserted based on this id laterUri rawContactUri = ().insert(.CONTENT_URI, values);
long rawContactId = (rawContactUri);
(.RAW_CONTACT_ID, rawContactId);
// Content Type(, .CONTENT_ITEM_TYPE);
// Contact name(.GIVEN_NAME, firstName);
(.FAMILY_NAME, lastName);
(.MIDDLE_NAME, middleName);
// Add a contact name to the contact URI().insert(.CONTENT_URI, values);

2. Jump to add

Add all rows above to the array and pass them together

List<ContentValues> values = new ArrayList<>();
//Add data to be set...
Intent intent = new Intent(Intent.ACTION_INSERT, .CONTENT_URI);
(, name);
(, values);
(intent);

3. Add to an existing contact

Add all rows above to the array and pass them together. After jumping, you need to select a contact

List<ContentValues> values = new ArrayList<>();
//Add data to be set...
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
(.CONTENT_ITEM_TYPE);
(, values);
startActivity(intent);

Permissions

The permissions cannot be forgotten, otherwise the error will be reported, namely the read and write permissions of the contact person.

    <uses-permission android:/>
    <uses-permission android:/>

The summary is quite detailed so far, and there are additional comments.

The above is the detailed content of how to add contacts to address book on Android. For more information about adding contacts to address book on Android, please follow my other related articles!