当前位置: 首页 > news >正文

android 字母好友,如何获取android手机联系人并按字母展示(二)

如何获取联系人的头像:

这里采用protected HashMap> mBitmapCache = new HashMap>();

protected HashSet mItemsMissingImages = new HashSet();

protected ImageLoaderHandler mHandler;

protected ImageLoader mImageFetcher;

这边是根据photoId去数据库里查询:public static Bitmap getContactPhoto(Context context, long photoId, BitmapFactory.Options options) {

if (photoId 

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

// Workaround for Android Issue 8488 http://code.google.com/p/android/issues/detail?id=8488

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;

}

这个图片是压缩两倍,再转成bitmap的,我们需要考虑如果联系人非常多,我们不能把联系人的所有

头像都实时获取,再把转换得到的bitmap放到imageView中

在getView中,如果我们快速滑动列表,那么肯定不能去做获取图片的操作if (mScrollState != OnScrollListener.SCROLL_STATE_FLING) {

sendFetchImageMessage(viewToUse);

}

在检测到scrollview停顿的时候我们再去获取@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);

}

}

然后图片的获取放到单独的线程中进行,通过线程池来管理:mImageFetcher = new ImageLoader(photoId, view);

synchronized (ContactsList.this) {

if (sImageFetchThreadPool == null) {

sImageFetchThreadPool = Executors.newFixedThreadPool(3);

}

sImageFetchThreadPool.execute(mImageFetcher);

}

如果在线程中获取到了bitmap那么我们通过handler来进行通知:Message msg = new Message();

msg.what = FETCH_IMAGE_MSG;

msg.obj = mImageView;

mHandler.sendMessage(msg);

然后在handlermessage中进行处理: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 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);

}

}

完整代码如下: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 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 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();

}

public void clearMessages() {

if (mHandler != null) {

try {

mHandler.removeCallbacksAndMessages(null);

} catch (java.lang.Throwable th) {

}

mHandler = null;

}

}

//image downloader

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 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 

return;

}

Bitmap photo = ContactsUtils.getContactPhoto(getBaseContext(),

mPhotoId, null);

if (photo == null) {

return;

}

mBitmapCache.put(mPhotoId, new SoftReference(photo));

if (Thread.interrupted()) {

return;

}

Message msg = new Message();

msg.what = FETCH_IMAGE_MSG;

msg.obj = mImageView;

mHandler.sendMessage(msg);

}

}

}//end of adapter

相关文章:

  • sony 播放器 android,Hi-Res Audio Player
  • adb更新android分区,Android adb升级OTA
  • android 反射执行方法,Android中怎么通过反射调用setUsbTethering()方法
  • iphone没有android安全,Android安全性与iPhone一样吗
  • android listview checkbox 选中状态,Android ListView专题之十  checkbox选中的值在翻屏时会跳来跳去...
  • 鸿蒙系统翻车了,鸿蒙系统翻车了?任正非承认忽视了关键问题,称华为犯下大错误...
  • android10侧滑返回黑屏,Android实现类似IOS右滑返回的效果(原因分析及解决办法)
  • 华为 android 安全,华为 EMUI/Magic UI 安全更新 2020-3
  • 华为mate50麒麟处理器鸿蒙系统,华为Mate50Pro概念图:有鸿蒙OS,处理器你选择麒麟还是高通?...
  • html右侧箭头是什么控件,html实现右箭头
  • html免费教程在线,免费HTML5在线教程 - 踏得网
  • android 单例设计模式,Android设计模式之单例模式
  • html5媒体查询的特点,HTML5 的媒体查询用多了好么?
  • html 数据校验,表单数据校验
  • 回收站有html文件,怎么找回回收站删除的文件 强烈推荐互盾专家上线支招
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 08.Android之View事件问题
  • android 一些 utils
  • Apache Pulsar 2.1 重磅发布
  • HomeBrew常规使用教程
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • JavaScript设计模式与开发实践系列之策略模式
  • Python进阶细节
  • Spring Cloud中负载均衡器概览
  • 欢迎参加第二届中国游戏开发者大会
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何设计一个微型分布式架构?
  • 从如何停掉 Promise 链说起
  • 湖北分布式智能数据采集方法有哪些?
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • $.ajax()
  • (1)bark-ml
  • (42)STM32——LCD显示屏实验笔记
  • (Python第六天)文件处理
  • (超详细)语音信号处理之特征提取
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (三)Honghu Cloud云架构一定时调度平台
  • (生成器)yield与(迭代器)generator
  • (转)Scala的“=”符号简介
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • .gitignore文件—git忽略文件
  • .net core使用ef 6
  • .NET MAUI学习笔记——2.构建第一个程序_初级篇
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .Net 代码性能 - (1)
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .Net开发笔记(二十)创建一个需要授权的第三方组件
  • @font-face 用字体画图标
  • @selector(..)警告提示
  • []使用 Tortoise SVN 创建 Externals 外部引用目录
  • [android学习笔记]学习jni编程