Android gets the contact avatar instance code based on the phone number
- 2020-06-07 05:15:38
- OfStack
In the daily use of Android mobile phone, it is a common problem to get the contact avatar according to the phone number. This article is an example of Android's implementation code for getting contact personas based on phone Numbers. To share with you for your reference. The specific methods are as follows:
First, data such as contacts in Android can be accessed through ContentProvider. Common Uri are:
Contact information Uri: content: / / com android. contacts/contacts
Contact telephone Uri: content: / / com android. contacts/data/phones
Contact email Uri: content: / / com android. contacts/data/emails
It also provides the function to obtain the data of data table according to the telephone number by data/phones/filter/ number, and returns 1 data set. Then get the contact's contact_id through the data set, open InputStream according to contact_id, and finally get the contact's avatar with ES43en.decodeStream ().
The specific function code is as follows:
// Get the contact profile picture based on the number
public static void get_people_image(String x_number){
// To obtain Uri
Uri uriNumber2Contacts = Uri.parse("content://com.android.contacts/"
+ "data/phones/filter/" + x_number);
// The query Uri , returns the data set
Cursor cursorCantacts = context.getContentResolver().query(
uriNumber2Contacts,
null,
null,
null,
null);
// If the contact exists
if (cursorCantacts.getCount() > 0) {
// Move to the first 1 The data
cursorCantacts.moveToFirst();
// Access to the contact contact_id
Long contactID = cursorCantacts.getLong(cursorCantacts.getColumnIndex("contact_id"));
// To obtain contact_id the Uri
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID);
// Open the profile picture InputStream
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
// from InputStream To obtain bitmap
bmp_head = BitmapFactory.decodeStream(input);
}<br>}
I hope this article has been helpful for your Android programming.