Android storage contact Summary

Source: Internet
Author: User
Post: http://www.ophonesdn.com/forum/thread-4969-1-1.html. the storage contacts on Android are settled. Since the storage contact has been changed after 2.0, the storage method is different. Now we post it:

(1) less than 2.0
Public class addcontactapi3 {

Public void addcontact (context, string name, string organization,
String phone, string fax, string email, string address,
String website, bitmap logo ){
Contentvalues personvalues = new contentvalues ();
Personvalues. Put (contacts. People. Name, name );
Personvalues. Put (contacts. People. starred, 1 );

Uri newpersonuri = context. getcontentresolver (). insert (
Contacts. People. content_uri, personvalues );

If (newpersonuri! = NULL ){

// Add Group

Long personid = contenturis. parseid (newpersonuri );
Contentvalues group = new contentvalues ();
Group. Put (groupmembership. person_id, personid );
Group. Put (groupmembership. group_id, 1 );
Uri groupupdate = context. getcontentresolver (). insert (
Groupmembership. content_uri, group );

// Add Company (organization)
If (! Apputils. isblank (organization )){
Contentvalues organisationvalues = new contentvalues ();
Uri orguri = URI. withappendedpath (newpersonuri,
Contacts. Organizations. content_directory );
Organisationvalues
. Put (contacts. Organizations. Company, organization );
Organisationvalues. Put (contacts. Organizations. type,
Contacts. Organizations. type_work );
Uri orgupdate = context. getcontentresolver (). insert (orguri,
Organisationvalues );
}

// Add mobile phone number
If (! Apputils. isblank (phone )){
Contentvalues mobilevalues = new contentvalues ();
Uri mobileuri = URI. withappendedpath (newpersonuri,
Contacts. People. Phones. content_directory );
Mobilevalues. Put (contacts. Phones. Number, phone );
Mobilevalues. Put (contacts. Phones. type, contacts. Phones. type_mobile );
Uri phoneupdate = context. getcontentresolver (). insert (mobileuri,
Mobilevalues );
}

// Add fax number
If (! Apputils. isblank (fax )){
Contentvalues faxvalues = new contentvalues ();
Uri faxuri = URI. withappendedpath (newpersonuri,
Contacts. People. Phones. content_directory );
Faxvalues. Put (contacts. Phones. number, fax );
Faxvalues. Put (contacts. Phones. type, contacts. Phones. type_fax_work );
Uri phoneupdate = context. getcontentresolver (). insert (faxuri, faxvalues );
}

// Add email
If (! Apputils. isblank (email )){
Contentvalues emailvalues = new contentvalues ();
Uri emailuri = URI. withappendedpath (newpersonuri,
Contacts. People. contactmethods. content_directory );
Emailvalues. Put (contacts. contactmethods. Kind, contacts. kind_email );
Emailvalues. Put (contacts. contactmethods. type,
Contacts. contactmethods. type_home );
Emailvalues. Put (contacts. contactmethods. Data, email );
Uri emailupdate = context. getcontentresolver ()
. Insert (emailuri, emailvalues );
}

// Add address
If (! Apputils. isblank (Address )){
Contentvalues addressvalues = new contentvalues ();
Uri addressuri = URI. withappendedpath (newpersonuri,
Contacts. People. contactmethods. content_directory );
Addressvalues. Put (contacts. contactmethods. kind,
Contacts. kind_postal );
Addressvalues. Put (contacts. contactmethods. type,
Contacts. contactmethods. type_home );
Addressvalues. Put (contacts. contactmethods. Data, address );
Uri addressupdate = context. getcontentresolver (). insert (addressuri,
Addressvalues );
}

// Add logo image
Bitmap Bm = logo;
If (BM! = NULL ){
Bytearrayoutputstream baos = new bytearrayoutputstream ();
BM. Compress (bitmap. compressformat. JPEG, 100, baos );
Byte [] photo = baos. tobytearray ();
If (photo! = NULL ){
People. setphotodata (context. getcontentresolver (), newpersonuri,
Photo );
}
}
}

}
}

(2) more than 2.0 can be stored in batches
Public class addcontactapi5 {

Public void addcontact (context, string name, string organization,
String phone, string fax, string email, string address,
String website, bitmap logo ){

Arraylist <contentprovideroperation> Ops = new arraylist <contentprovideroperation> ();

Ops. Add (contentprovideroperation. newinsert (contactscontract. rawcontacts. content_uri)
. Withvalue (contactscontract. rawcontacts. account_type, null)
. Withvalue (contactscontract. rawcontacts. account_name, null)
. Withvalue (contactscontract. rawcontacts. aggregation_mode, contactscontract. rawcontacts. aggregation_mode_disabled)
. Build ());

// Add name
If (! Apputils. isblank (name )){
Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. structuredname. content_item_type)
. Withvalue (contactscontract. commondatakinds. structuredname. display_name, name)
. Build ());
}

// Add Company
If (! Apputils. isblank (organization )){
Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. Organization. content_item_type)
. Withvalue (contactscontract. commondatakinds. Organization. Company,
Organization)
. Withvalue (contactscontract. commondatakinds. Organization. type,
Contactscontract. commondatakinds. Organization. type_work)
. Build ());
}

// Add phone
If (! Apputils. isblank (phone )){
Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. Phone. content_item_type)
. Withvalue (contactscontract. commondatakinds. Phone. Number, phone)
. Withvalue (contactscontract. commondatakinds. Phone. type, 1)
. Build ());
}

// Add Fax
If (! Apputils. isblank (fax )){
Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. Phone. content_item_type)
. Withvalue (contactscontract. commondatakinds. Phone. number, fax)
. Withvalue (contactscontract. commondatakinds. Phone. type,
Contactscontract. commondatakinds. Phone. type_fax_work)
. Build ());
}

// Add email
If (! Apputils. isblank (email )){
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, email)
. Withvalue (contactscontract. commondatakinds. Email. type, 1)
. Build ());
}

// Add address
If (! Apputils. isblank (Address )){
Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. structuredpostal. content_item_type)
. Withvalue (contactscontract. commondatakinds. structuredpostal. Street, address)
. Withvalue (contactscontract. commondatakinds. structuredpostal. type,
Contactscontract. commondatakinds. structuredpostal. type_work)
. Build ());
}

// Add website
If (! Apputils. isblank (website )){
Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. Website. content_item_type)
. Withvalue (contactscontract. commondatakinds. Website. url, website)
. Withvalue (contactscontract. commondatakinds. Website. type,
Contactscontract. commondatakinds. Website. type_work)
. Build ());
}

// Add logo image
Bitmap Bm = logo;
If (BM! = NULL ){
Bytearrayoutputstream baos = new bytearrayoutputstream ();
BM. Compress (bitmap. compressformat. PNG, 100, baos );
Byte [] photo = baos. tobytearray ();
If (photo! = NULL ){

Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. Photo. content_item_type)
. Withvalue (contactscontract. commondatakinds. Photo. photo, photo)
. Build ());

}
}

Try {
Context. getcontentresolver (). applybatch (contactscontract. Authority, OPS );
} Catch (exception e ){

}

}
}

(3) put forward the key things in the above two methods:
1) less than 2.0 of the data is stored in a fixed group, and this survey has taken a long time to rise:
Long personid = contenturis. parseid (newpersonuri );
Contentvalues group = new contentvalues ();
Group. Put (groupmembership. person_id, personid );
Group. Put (groupmembership. group_id, 1 );
Uri groupupdate = context. getcontentresolver (). insert (
Groupmembership. content_uri, group );

2) The following 2.0 storage contact pictures:
Bitmap Bm = logo;
If (BM! = NULL ){
Bytearrayoutputstream baos = new bytearrayoutputstream ();
BM. Compress (bitmap. compressformat. JPEG, 100, baos );
Byte [] photo = baos. tobytearray ();
If (photo! = NULL ){
People. setphotodata (context. getcontentresolver (), newpersonuri,
Photo );
}

3) more than 2.0 of Contact Image Storage Methods:
Bitmap Bm = logo;
If (BM! = NULL ){
Bytearrayoutputstream baos = new bytearrayoutputstream ();
BM. Compress (bitmap. compressformat. PNG, 100, baos );
Byte [] photo = baos. tobytearray ();
If (photo! = NULL ){

Ops. Add (contentprovideroperation. newinsert (contactscontract. Data. content_uri)
. Withvaluebackreference (contactscontract. Data. raw_contact_id, 0)
. Withvalue (contactscontract. Data. mimetype,
Contactscontract. commondatakinds. Photo. content_item_type)
. Withvalue (contactscontract. commondatakinds. Photo. photo, photo)
. Build ());

}

4) more than 2.0 of critical points:
. Withvalue (contactscontract. rawcontacts. aggregation_mode, contactscontract. rawcontacts. aggregation_mode_disabled)

In this case, the contacts with the same name are stored separately. Otherwise, an error is returned when a contact is edited after being called twice. This attribute is not described on the official API and is very important.

(4) Of course, both 1.5 and 2.0 can be called to the system's edit contact through intent:
Intent it = new intent (contacts. intents. Insert. Action, contacts. People. content_uri );
It. putextra (Android. provider. Contacts. intents. Insert. Name, firstname );
It. putextra (Android. provider. Contacts. intents. Insert. Company, organization );
It. putextra (Android. provider. Contacts. intents. Insert. job_title, title );
It. putextra (Android. provider. Contacts. intents. Insert. Email, workemail );
It. putextra (Android. provider. Contacts. intents. Insert. secondary_email, homeemail );
It. putextra (Android. provider. Contacts. intents. Insert. Phone, mobilephone );
It. putextra (Android. provider. Contacts. intents. Insert. phone_type, contacts. phonescolumns. type_mobile );
It. putextra (Android. provider. Contacts. intents. Insert. secondary_phone, homephone );
It. putextra (Android. provider. Contacts. intents. Insert. secondary_phone_type,
Contacts. phonescolumns. type_home );
It. putextra (Android. provider. Contacts. intents. Insert. tertiary_phone, workphone );
It. putextra (Android. provider. Contacts. intents. Insert. tertiary_phone_type,
Contacts. phonescolumns. type_work );
It. putextra (Android. provider. Contacts. intents. Insert. Postal, homeaddress );
Startactivity (it); Post: http://www.ophonesdn.com/forum/thread-4969-1-1.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.