Android access to contact Avatar method _android

Source: Internet
Author: User
Tags sqlite database

This example describes how Android gets the avatar of a contact. Share to everyone for your reference, specific as follows:

Public byte[] Getphoto (string people_id) {
string photo_id = null;
String Selection1 = contactscontract.contacts._id + "=" + people_id;
Cursor cur1 = Getcontentresolver (). Query (
ContactsContract.Contacts.CONTENT_URI, NULL, selection1, NULL, NULL);
if (Cur1.getcount () > 0)
{
cur1.movetofirst ();
photo_id = cur1.getstring (Cur1.getcolumnindex (ContactsContract.Contacts.PHOTO_ID));
System.out.println ("photo_id:" + photo_id);
}
string[] projection = new string[]
{
ContactsContract.Data.DATA15
};
String selection = contactscontract.data._id + "=" + photo_id;
Cursor cur = getcontentresolver (). Query (
ContactsContract.Data.CONTENT_URI, projection, selection, NULL, NULL); C18/>cur.movetofirst ();
byte[] Contacticon = Cur.getblob (0);
System.out.println ("Contacticon:" + Contacticon);
if (Contacticon = = null) {return
null;
} else {return
Contacticon
;
}}

The following code converts a byte array into a Bitmap object and then displays it in ImageView

Private ImageView image;
byte[] photo = Getphoto (ContactID);
Bitmap map = Bitmapfactory.decodebytearray (photo, 0,photo.length);
Image.setimagebitmap (map);

To set up an Android contact's avatar by code:

Private Final Static Boolean oldsdk = (System.getsdkversionnumber () < 5)?
True:false;  public static void Setpersonphotobytes (context context,byte[] B, Long Persionid, Boolean Sync) {if (OLDSDK) {Uri MyPerson
= Contenturis.withappendedid (People.content_uri, Persionid);
People.setphotodata (Context.getcontentresolver (), MyPerson, b); if (! Sync) {contentvalues values = new Contentvalues (); Values.put ("_sync_dirty", 0); Context.getcontentresolver (). Update (
MyPerson, values, NULL, NULL);
} else SetContactPhoto5 (Context.getcontentresolver (), B, Persionid, Sync);  } private static void SetContactPhoto5 (Contentresolver C, byte[] Bytes,long personId, Boolean Sync) {contentvalues values
= new Contentvalues ();
Uri u = uri.parse ("Content://com.android.contacts/data");
int photorow =-1;
String where = "raw_contact_id =" + PersonId + "and mimetype = ' Vnd.android.cursor.item/photo '";
Cursor Cursor = c.query (U, NULL, where, NULL, NULL);
int ididx = Cursor.getcolumnindexorthrow ("_id"); if (curSor.movetofirst ()) {Photorow = Cursor.getint (IDIDX);} cursor.close ();
Values.put ("raw_contact_id", personId);
Values.put ("Is_super_primary", 1);
Values.put ("Data15", bytes);
Values.put ("MimeType", "Vnd.android.cursor.item/photo"); if (photorow >= 0) {c.update (U, Values, "_id=" + photorow, null);} else {C.insert (U, values);} if (! Sync) {u = Uri.withappendedpath (Uri.parse ("Content://com.android.contacts/raw_contacts"), String.valueof (personId)
);
values = new Contentvalues ();
Values.put ("dirty", 0);
C.update (U, values, NULL, NULL);

 }
}

More interested readers of Android-related content can view the site: Android Development Primer and Advanced tutorials, the Android View View tips Summary, the activity tips summary of Android programming, Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

Related Article

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.