如何获取android手机联系人并按字母展示(三)

如果获取contact的头像信息并展示:

如何根据photoId来获取bitmap:

public static Bitmap getContactPhoto(Context context, long photoId, BitmapFactory.Options options) {
        if (photoId < 0) {
            return null;
        }

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(
                    ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId),
                    new String[] { Photo.PHOTO }, null, null, null);

            if (cursor != null && cursor.moveToFirst() && !cursor.isNull(0)) {
                byte[] photoData = cursor.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;
    }


在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) {
						mBitmapCache.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);
					}
				}
			}
		}

获取的头像的方法:

	private class ImageLoaderHandler extends Handler {
			@Override
			public 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);
			}
		}

下载头像可以起线程池:

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();
		}

我们可以对下载做优化,在列表静止的时候才去下,这个我们让adatper继承OnScrollListener,这样有两个重载函数:

<span style="white-space:pre">		</span>@Override
		public 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);
			}
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			
		}

在activity销毁的时候,我们得去释放资源:

@Override
	protected 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=8488
		if (mAdapter != null && mAdapter.mBitmapCache != null) {
			for (SoftReference<Bitmap> bitmap : mAdapter.mBitmapCache.values()) {
				if (bitmap != null && bitmap.get() != null) {
					bitmap.get().recycle();
					bitmap = null;
				}
			}
			mAdapter.mBitmapCache.clear();
		}

	}

代码: http://download.csdn.net/detail/baidu_nod/7774203

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值