【问题标题】:android easy way to check contact has imageandroid检查联系人的简单方法有图像
【发布时间】:2014-11-25 09:24:16
【问题描述】:

我只是使用通用图像加载器在列表视图上显示联系人图像。我只是得到这样的照片 uri。

public String getPhotoUri(String contactId) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactId);
    return uri + "";
}

并像这样在列表视图中显示:

 ImageLoader.getInstance().displayImage(getPhotoUri(item.getContactId()), holder.ivHead);

效果很好。但我的问题是当联系人没有图像时。我会在下面得到错误:

java.lang.NullPointerException
        at com.nostra13.universalimageloader.utils.IoUtils.copyStream(IoUtils.java:69)
        at com.nostra13.universalimageloader.cache.disc.impl.BaseDiscCache.save(BaseDiscCache.java:109)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:291)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisk(LoadAndDisplayImageTask.java:273)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:229)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:841)

它不会崩溃。我可以在 image-univeral-loader 上使用 showImageOnFail 方法来显示默认图像。但我讨厌 NPE 。当我滚动时,几乎所有的日志都是 NPE。 所以我只想知道如何检查联系人是否有图像。我试着用这种方式

    public String getPhotoUri(String contactId) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
    InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(),uri);
    if (null !=is){
        return uri+"";
    }else {
        //TODO
    }
    return uri + "";
}

但是 ContactsContract.Contacts.openContactPhotoInputStream 只是对系统联系人数据库进行联系人查询。所以如果我想显示所有的联系人照片。它会查询很多次。那么有什么好的方法来检查联系人是否有图像吗?

【问题讨论】:

  • 你想通过通用加载简单地显示图像吗?对吗?

标签: android image contact


【解决方案1】:

方法一:
检索图像 URI 并检查它是否为 NULL。

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String image_uri = "";

现在获取图片 URI,

image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (image_uri != null) {  
  // set your image
} else {   // set default image
}

方法二:
尝试这样检索联系人照片:

public Uri getPhotoUri(int contactid) {
  Cursor photoCur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'", null, ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
  photoCur.moveToPosition(contactid);
  Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, photoCur.getLong(photoCur.getColumnIndex(ContactsContract.Contacts._ID)));
  Uri photo = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
  return photo;
}

现在用这个来设置图片,(contactimage是一个ImageView):

Uri contactphoto = getPhotoUri(contact_id);
contactimage.setImageURI(contactphoto);
if (contactimage.getDrawable() == null) {  // If no image is found.
   contactimage.setImageResource(R.drawable.defaultImage);
}

【讨论】:

  • @sunday 你觉得ans 有用吗?
  • 我只是使用方法1来知道是否有图像。并使用方法2得到它。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-09-15
  • 2014-08-15
  • 2018-09-16
  • 2021-04-22
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
相关资源
最近更新 更多