SoFunction
Updated on 2025-03-02

Android obtains contact avatar instance code based on phone number

In daily use of Android phones, obtaining contact avatars based on the phone number is a common problem. This article describes the implementation code of Android's implementation of the contact avatar based on the phone number. Share it for your reference. The specific methods are as follows:

First of all, through ContentProvider, you can access contacts and other data in Android. Commonly used Uri are:

Contact information Uri: content:///contacts
Contact number Uri: content:///data/phones
Contact email Uri: content:///data/emails

It also provides the function of obtaining data table data based on the phone number. The method is: data/phones/filter/number, returning a data set. Then obtain the contact_id of the contact through the data set, open the InputStream of the avatar picture based on the contact_id, and finally use () to obtain the contact's avatar.

The specific function codes are as follows:

// Obtain contact profile picture based on the numberpublic static void get_people_image(String x_number){  
 
  // Get Uri  Uri uriNumber2Contacts = ("content:///"
      + "data/phones/filter/" + x_number); 
  // Query Uri and return the dataset  Cursor cursorCantacts = ().query(
      uriNumber2Contacts, 
      null, 
      null,            
      null, 
      null);
  // If the contact exists  if (() > 0) { 
    // Move to the first piece of data    ();
    // Get the contact_id of the contact     Long contactID = (("contact_id"));
    // Get Uri with contact_id     Uri uri = (.CONTENT_URI, contactID);
    // Open the InputStream of the avatar image    InputStream input = ((), uri); 
    // Get bitmap from InputStream    bmp_head = (input);   
}<br>}

I hope this article will be helpful to everyone's Android programming design.