Android obtains the contact information in the address book. The Code is as follows:
/*** Get the contact information, including the name, number, and profile * @ return */private arraylist <phonecontactor> getphonecontacts () {log. I (TAG, "getphonecontacts"); contentresolver resolver = This. getcontentresolver (); arraylist <phonecontactor> List = new arraylist <phonecontactor> (); string [] Columns = new string [] {Phone. display_name, phone. number, phone. photo_id, phone. contact_id}; cursor = resolver. query (phone. content_uri, columns, null); bitmap photo_default = bitmapfactory. decoderesource (getresources (), R. drawable. contact_photo_default); While (cursor. movetonext () {phonecontactor Pc = new phonecontactor (); string name = cursor. getstring (cursor. getcolumnindex (phone. display_name); string number = cursor. getstring (cursor. getcolumnindex (phone. number); If (textutils. isempty (number) {// determine whether the number is blank continue;} Long photoid = cursor. getlong (cursor. getcolumnindex (phone. photo_id); long contactid = cursor. getlong (cursor. getcolumnindex (phone. contact_id); // obtain the Contact Profile bitamp bitmap contactphoto = NULL; // If the photoid is greater than 0, the contact has an avatar; otherwise, set a default profile if (photoid> 0) {URI uri = contenturis. withappendedid (contactscontract. contacts. content_uri, contactid); inputstream input = contactscontract. contacts. opencontactphotoinputstream (Resolver, Uri); contactphoto = bitmapfactory. decodestream (input);} else {contactphoto = photo_default;} PC. name (name); PC. number (number); PC. photo (contactphoto); list. add (PC); log. D (TAG, "Name:" + PC. name (); log. D (TAG, "Number:" + PC. number ();} cursor. close (); return list ;}
Here: phonecontactor is a custom class for storing contact information. Do not forget to add the permission:
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>