Android gets contact Avatar instance code based on phone number _android

Source: Internet
Author: User

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.

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.