如何检查联系人是否有图片?

6

我正在使用以下代码将联系人图像分配给 ImageView:

mPhotoView = (ImageView) findViewById(R.id.photo);
mPhotoView.setImageURI(objItem.getPhotoUri());

如果联系人没有图片,则不执行任何操作,也不会引发错误。
当没有图片时,我想添加一个默认图片。因此,我需要检查视图是否已添加图像,或者检查URI是否包含一些图像数据。
我该如何实现这一点?
然后我将通过以下方式设置默认图像:
mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);

你是针对爱克莱尔还是卡片/多拿特? - Schildmeijer
4个回答

9
如果您的目标设备运行的是Android 2.0/2.0.1/2.1,则必须使用类似以下选择条件查询ContactsContract.Data.CONTENT_URI:
Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE

否则查询Contacts.Photos.CONTENT_URI。 由Pentium10编辑 为了参考,我在此处包含我想出的方法(如果您仍然看到错误,请更新它):
public Uri getPhotoUri() {
    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.parseLong(getId()));
    Uri photo = Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

    Cursor cur = this.ctx
            .getContentResolver()
            .query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID
                            + "="
                            + this.getId()
                            + " AND "
                            + ContactsContract.Data.MIMETYPE
                            + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                            + "'", null, null);
    if (cur != null) {
        if (!cur.moveToFirst()) {
            return null; // no photo
        }
    } else {
        return null; // error in cursor process
    }
    return photo;
}

这不是一个详细的解释,但如果你熟悉ContentProviders/ContentResolvers,这应该足够了。否则请告诉我,我会发布一些代码示例。 - Schildmeijer
谢谢。我编辑了你的答案并加入了我想出的解决方案。请检查是否有错误。 - Pentium10
1
由于某些原因,有时如果联系人具有更多详细信息,则返回URI,但URI没有图像。 - Hamidreza Sadegh

0

这是我根据其他答案得出的结果。

    private Uri getUserPictureUri(long id) {
        Uri person = ContentUris.withAppendedId(
        ContactsContract.Contacts.CONTENT_URI, id);
        Uri picUri = Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        try {
            InputStream is = getContentResolver().openInputStream(picUri);
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return picUri;
}

0

只需检查PhoneLookUp内容提供程序中的PHOTO_ID列是否为空 这是我开发的一个方法:

public static boolean hasContactPhoto(Context context, String number) {
    String thumbUri = "";
    String photoId = "";
    String id = "";
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(number));
    Cursor cursor = context.getContentResolver().query(uri,
            new String[] {ContactsContract.PhoneLookup._ID,ContactsContract.PhoneLookup.PHOTO_ID}, null, null, null);
    if (cursor.moveToFirst()) {
        id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
        photoId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_ID));
    }
    cursor.close();
    if(!id.equals("") && photoId != null && !photoId.equals(""))
        return true;
        //sms.setContactThumb(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id)).toString());
    else
        return false;
}

如果您需要联系人图像 URI,请从以下位置获取:

ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(id)).toString()

如果您需要将其设置为图像视图,请使用以下行(我已经使用 Universal Image Loader 1.9.2 进行图像加载):

        ImageLoader.getInstance().displayImage(currentRecord.getContactThumb(),viewHolder.mThumbIV);

注意:contactThumb是最后描述的URI。 干杯!


0
根据您的使用情况,可能会有另一个(更好的?)选项。如果您有所有联系人的光标,并且您正在将它们绑定到列表视图,则可以尝试使用openContactPhotoInputStream检索照片,并检查是否为null。
InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri);
if (stream == null) {
    //your logic here when no photo found
}

以下是一个简单的示例,供任何可能遇到此情况的人参考:

import android.provider.ContactsContract.Contacts;

//Define someDefaultPhoto somewhere accessible

protected Bitmap getContactPhoto(Context context, Uri lookupUri) {
    Bitmap resultPhoto;
    InputStream stream;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        //For some reason this seems to be necessary pre-ICS. If anyone knows why, I'd love to hear
        lookupUri = Contacts.lookupContact(context.getContentResolver(), lookupUri);
    }
    stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), lookupUri);

    if (stream == null) {
        resultPhoto = someDefaultPhoto;
    }
    else {
        resultPhoto = BitmapFactory.decodeStream(stream);
    }
    return resultPhoto;
}

然后你可以像这样调用它:

int columnLookupKey = cursor.getColumnIndex(Contacts.LOOKUP_KEY);
String lookupKey = cursor.getString(columnLookupKey);
Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
Bitmap contactPhoto = getContactPhoto(context, lookupUri);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接