检测,如果联系人有照片 [英] detect if contact has photo

查看:197
本文介绍了检测,如果联系人有照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我敢显示联系人图片使用URI总是类似于这样一个ImageView的:

I've got an ImageView which I'm displaying a contacts picture using a Uri which always looks similar to this:

内容://com.android.contacts/contacts/34/photo

content://com.android.contacts/contacts/34/photo

我如何能够检测这张照片是否存在,因为如果它不那么我想用一个占位符代替(存储在我的绘制文件夹)。目前,它只是显示一个空白图像。

How would I be able to detect whether this photo exists, as if it doesn't then I want to use a placeholder instead (stored in my drawable folder). At the moment it just shows a blank image.

推荐答案

一个函数来获取联系人的照片URI:

a function to get a contacts photo uri:

public Uri getPhotoUri(Integer 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的):

and calling that function (contactimage is an ImageView):

Uri contactphoto = getPhotoUri(2);
contactimage.setImageURI(contactphoto);
if (contactimage.getDrawable() == null) {
    contactimage.setImageResource(R.drawable.contactplaceholder);
}

这篇关于检测,如果联系人有照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆