How Android Gets the original image of the local address book

Source: Internet
Author: User

In the last few words of contact, the avatar is in the database, through:

cursor = Context.getcontentresolver (). Query (                      contenturis.withappendedid (ContactsContract.Data.CONTENT_URI, photoid),                      new string[] {Photo.photo}, NULL, NULL, NULL);  

To get


In fact, if we in the local address book, if a contact Gakou or take photos of the picture, then the system will be a clear picture of the image stored, if you want to take this picture, you need to ContactID

Getlistview (). Setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<? > Parent, view view, int position, long id) {try {cursor cursor = (cursor) getlistadapter (). GetItem (position); if (curso r = = null) {return;} int contactId = Cursor.getint (personal.id_column_index); Intent Intent = new Intent (); Intent.setclass ( Contactslist.this, Contactdetail.class), Intent.putextra ("ContactId", contactId); startactivity (intent);} catch (Exception ex) {ex.printstacktrace ();}}});


If we want the picture to have rounded corners, this can be done:

 <linearlayout android:layout_width= "Fill_parent" android:layout_height = "Fill_parent" android:gravity= "center" > <framelayout android:id= "@+id/contacts_lo Go_layout "android:layout_width=" @dimen/incall_photo_view_size "android:layout_height=" @dimen/incall _photo_view_size "> <imageview android:id=" @+id/contact_photo "Android:layo                Ut_width= "@dimen/incall_photo_view_size" android:layout_height= "@dimen/incall_photo_view_size" /> <imageview android:id= "@+id/contacts_logoborder" android:layout_width = "@dimen/incall_photo_view_size" android:layout_height= "@dimen/incall_photo_view_size" Andro id:background= "@drawable/images_box" > </ImageView> </FrameLayout> </linear Layout> 

Java code:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.contact_detail); int contactId = GetIntent ()    . Getintextra ("ContactId", 0); Contact_photo = (ImageView) Findviewbyid (R.id.contact_photo); Loadtask task = new Loadtask (contactId); Task.execute ();} Private class Loadtask extends Asynctask<void, Void, bitmap>{public loadtask (int id) {contactId = ID;} private int contactId; @Overrideprotected Bitmap doinbackground (Void ... params) {InputStream InputStream = Opendisplayphoto (CONTACTID); Bitmapfactory.options opt = new bitmapfactory.options (); opt.insamplesize = 1; Bitmap Bitmap = Bitmapfactory.decodestream (InputStream, NULL, opt); return Bitmap;} @Overrideprotected void OnPostExecute (Bitmap result) {if (result! = null) {Contact_photo.setimagebitmap (result);} Super.onpostexecute (result);}} Public InputStream Opendisplayphoto (long contactId) {<strong>//This is the code that takes the InputStream of the clear diagram </strong> Uri Contacturi = Contenturis.withappendedid (Contacts.content_uri, contactId);     Uri Displayphotouri = Uri.withappendedpath (Contacturi, Contacts.Photo.DISPLAY_PHOTO);         try {assetfiledescriptor fd = This.getcontentresolver (). Openassetfiledescriptor (Displayphotouri, "R");     return Fd.createinputstream ();         } catch (IOException e) {e.printstacktrace ();     return null; } }

:


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.