In the daily use of the Android phone, the phone number to get a contact avatar, is often encountered problems. This article is an example of how Android is getting contact with a phone number to get a head image of the implementation code. Share for everyone to use for reference. The specific methods are as follows:
First, through ContentProvider, you can access data such as contacts in Android. The common URIs are:
Contact Information uri:content://com.android.contacts/contacts
Contact Phone Uri:content://com.android.contacts/data/phones
Contact Mail Uri:content://com.android.contacts/data/emails
It also provides the ability to obtain data from a data table based on a phone number by: data/phones/filter/number, which returns a DataSet. Then through the dataset to get the contact's contact_id, according to contact_id open avatar picture InputStream, and finally with Bitmapfactory.decodestream () Get contact person's head.
The specific function code is as follows:
According to the number get contact head like public static void Get_people_image (String x_number) {//get URI uri urinumber2contacts = uri.parse (
"content://com.android.contacts/" + "data/phones/filter/" + x_number);
The query URI, which returns the dataset Cursor Cursorcantacts = Context.getcontentresolver (). query (urinumber2contacts, NULL,
NULL, NULL, NULL);
If the contact exists if (Cursorcantacts.getcount () > 0) {//move to the first Data Cursorcantacts.movetofirst ();
Get the contact's contact_id Long ContactID = Cursorcantacts.getlong (Cursorcantacts.getcolumnindex ("contact_id")); Gets the URI uri of the contact_id = Contenturis.withappendedid (ContactsContract.Contacts.CONTENT_URI, Contacti
D); Open the Avatar picture inputstream inputstream input = ContactsContract.Contacts.openContactPhotoInputStream (context.getconte
Ntresolver (), URI);
Obtain Bitmap Bmp_head = Bitmapfactory.decodestream (input) from InputStream;
}<br>}
I hope this article will help you with your Android program.