有名字和照片的URI联系方式查询 [英] Contacts query with name and picture URI

查看:114
本文介绍了有名字和照片的URI联系方式查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到一个单一的查询会给我的联系人API联系人的图像的URI和显示名称的API 2.0。

I couldn't find a single query that would give me in API 2.0 of the contacts API the URI of the contact's image and the display name.

现在据我知道我可以通过其联系人的_ID创建一个URI,但我没有看到,我可以在数据或联系的投影用得到的一切,我不需要任何列名。

For now as far as i know i can create a URI by having the contact's _ID , but i didn't see any row name that i can use in the projection of Data or Contact to get all that i need.

(我指的是使用名片API的API 2 Android SDK中V5及以上)

(i refer to using API 2 of the contacts API on android SDK V5 and above )

10倍。

推荐答案

这个方法返回的照片URI或为空,如果不存在确定的getId()

This method returns the photo Uri or null if does not exists for a contact identified by getId()

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

如果你添加一个投影光标返回ContactsContract.Data.CONTACT_ID和ContactsContract.Data.DISPLAY_NAME,也许你就会有有照片集的联系人列表。 (并非所有的触点)。然后为每个接触就可以计算在照片像的方法中的开始的URI。

If you add a projection to the Cursor to return ContactsContract.Data.CONTACT_ID, and ContactsContract.Data.DISPLAY_NAME, probably you will end up having a list of contacts that have photo set. (not all of the contacts). Then for each contact you can compute the Uri of the photo like in the beginning of the method.

这篇关于有名字和照片的URI联系方式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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