How to get contacts for Android phone and show by Alphabet (iii)

Source: Internet
Author: User

If you get contact's avatar information and show:

How to get bitmap according to PhotoID:

public static Bitmap Getcontactphoto (context context, long photoid, bitmapfactory.options Options) {if (PhotoID &L T        0) {return null;        } cursor cursor = NULL; try {cursor = Context.getcontentresolver (). Query (Contenturis.withappendedid (Contactscontra Ct.            Data.content_uri, PhotoID), new string[] {Photo.photo}, NULL, NULL, NULL); if (cursor! = NULL && cursor.movetofirst () &&!cursor.isnull (0)) {byte[] Photodata = curso                               R.getblob (0);                if (options = = null) {options = new bitmapfactory.options ();                } options.intempstorage = new byte[16 * 1024];                Options.insamplesize = 2;            Return Bitmapfactory.decodebytearray (photodata, 0, photodata.length, options); }} catch (Java.lang.Throwable error) {} finally {if (cursor! = NULL) {               Cursor.close ();    }} return null; }


Add in BindView:

protected void Setcontactphoto (cursor cursor, final ImageView viewtouse, int column) {Long photoid = 0;if (!cursor.isnull ( column) {photoid = cursor.getlong (column);} final int position = Cursor.getposition (); Viewtouse.settag (new Photoinfo (position, photoid)); if (photoid = = 0) { Viewtouse.setimageresource (R.drawable.avatar);} else {Bitmap photo = null; Softreference<bitmap> ref = Mbitmapcache.get (PhotoID), if (ref! = null) {photo = Ref.get (); if (photo = = null) {MBITMA Pcache.remove (photoid);}} if (photo! = null) {Viewtouse.setimagebitmap (photo);} else {viewtouse.setimageresource (R.drawable.avatar); Mitemsmissingimages.add (Viewtouse); if (mscrollstate! = onscrolllistener.scroll_state_fling) {sendFetchImageMessage (Viewtouse);}}}

To get the Avatar's method:

Private class Imageloaderhandler extends Handler {@Overridepublic void handlemessage (Message message) {if (isfinishing () ) {return;} Switch (message.what) {case fetch_image_msg: {final ImageView ImageView = (ImageView) message.obj;if (ImageView = = null) { break;} Final Photoinfo info = (photoinfo) imageview.gettag (); if (info = = null) {break;} Final Long photoid = info.photoid;if (PhotoID = = 0) {break;} softreference<bitmap> photoref = Mbitmapcache.get (photoid); if (photoref = = null) {break;} Bitmap photo = Photoref.get (); if (photo = = null) {mbitmapcache.remove (photoid); break;} Synchronized (ImageView) {final Photoinfo Updatedinfo = (photoinfo) imageview.gettag (); Long currentphotoid = Updatedinfo . photoid;if (Currentphotoid = = photoid) {imageview.setimagebitmap (photo); Mitemsmissingimages.remove (ImageView);} else {}}break;}}} public void clearimagefecthing () {removemessages (fetch_image_msg);}} Private class Imageloader implements Runnable {long mphotoid;private ImageView mimageview;public ImagEloader (Long photoid, ImageView ImageView) {this.mphotoid = Photoid;this.mimageview = ImageView;} public void Run () {if (isfinishing ()) {return;} if (thread.interrupted ()) {return;} if (Mphotoid < 0) {return;} Bitmap photo = Contactsutils.getcontactphoto (Getbasecontext (), mphotoid, NULL), if (photo = = null) {return;} Mbitmapcache.put (mphotoid, new softreference<bitmap> (photo)), if (thread.interrupted ()) {return;} Message msg = new Message (); msg.what = Fetch_image_msg;msg.obj = Mimageview;mhandler.sendmessage (msg);}}

Download avatar can play thread pool:

private void Processmissingimageitems (Abslistview view) {for (ImageView iv:mitemsmissingimages) { Sendfetchimagemessage (iv);}} protected void Sendfetchimagemessage (ImageView view) {Final Photoinfo info = (photoinfo) view.gettag (); if (info = = null) { return;} Final Long photoid = info.photoid;if (PhotoID = = 0) {return;} Mimagefetcher = new Imageloader (photoid, view); synchronized (contactslist.this) {if (Simagefetchthreadpool = = null) { Simagefetchthreadpool = Executors.newfixedthreadpool (3);} Simagefetchthreadpool.execute (Mimagefetcher);}} public void clearimagefetching () {synchronized (contactslist.this) {if (Simagefetchthreadpool! = null) { Simagefetchthreadpool.shutdownnow (); simagefetchthreadpool = null;}} Mhandler.clearimagefecthing ();}

We can optimize the download and go down when the list is stationary, we let adatper inherit Onscrolllistener, so there are two overloaded functions:

<span style= "White-space:pre" ></span> @Overridepublic void onscrollstatechanged (abslistview view, int Scrollstate) {if (Scroll_state_touch_scroll = = scrollstate) {                View currentfocus = Getcurrentfocus ();                if (currentfocus! = null) {                    currentfocus.clearfocus ();}            } Mscrollstate = scrollstate;if (scrollstate = = onscrolllistener.scroll_state_fling) {clearimagefetching ();} else { Processmissingimageitems (view);}} @Overridepublic void Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {}

When the activity is destroyed, we have to release the resources:

@Overrideprotected void OnDestroy () {Super.ondestroy (); if (Mqueryhandler! = null) {mqueryhandler.canceloperation ( Query_token); mqueryhandler = null;} if (Madapter = null && madapter.mitemsmissingimages! = null) {mAdapter.mItemsMissingImages.clear (); Madapter.clearmessages ();} Workaround for Android Issue 8488//http://code.google.com/p/android/issues/detail?id=8488if (madapter! = null && Amp Madapter.mbitmapcache! = null) {for (softreference<bitmap> bitmap:mAdapter.mBitmapCache.values ()) {if (Bitmap! = Null && bitmap.get () = null) {Bitmap.get (). recycle (); bitmap = null;}} MAdapter.mBitmapCache.clear ();}}

Code: http://download.csdn.net/detail/baidu_nod/7774203

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.