Android的获取联系人的照片从data.Email查询联系人、照片、Android、data

2023-09-07 09:27:33 作者:听腻了情话

我想提出一个自动完成字段查询按显示名称和电子邮件联系人。当有人点击该联系人添加到列表中与他的电子邮件,显示名称和图片过滤后所需的联系人,如果他有什么。

I am making a autocomplete Field that queries contacts by Display name and Email. When someone clicks on the desired contact after the filtering that contact is added to a list with his email, display name and Picture if he has any.

所以到目前为止,我已经成功地做​​到一切,除了让照片出现。下面是我运行查询来获取电子邮件,显示姓名,身份证和照片的身份证。

So so far i have managed to do everything except to make the Photo appear. Here is how i run the query to get the email, display name, ID , and Photo ID.

    return mContent.query(Email.CONTENT_URI,
              PROJECTION, filter, null, null);

在这里的投影为:

where projection is:

 PROJECTION = new String[] {ContactsContract.Contacts._ID,
              ContactsContract.Contacts.DISPLAY_NAME,
              ContactsContract.Contacts.PHOTO_ID,
                Email.DATA
            };

这人做什么,我需要的,返回的所有数据。但有一点我在调试这个问题的注意的是,在接触ID是,如果你运行针对ContactsContract.Contacts.CONTENT_URI的查询,例如一个特定的显示名称不同于。

This one does what i need and returns all the data. But one thing i noticed during debugging this issue is that the contact id is different than if you run the query against ContactsContract.Contacts.CONTENT_URI for a specific display name for example.

例如我已经运行测试,在那里我得到的所有联系人通过运行Contacts.CONTENT_URI给我用152的图像和ID的接触。然而对Email.CONTENT_URI查询给了我452的一个id同样的接触(有相同的显示名称和电子邮件地址)。所以,当我试图让照片包含了ID:452它返回的照片犯规存在的内容URI,但如果我尝试获取照片152它完美的作品。

For example the tests i have run where i get all the contacts by running the Contacts.CONTENT_URI gave me a contact with an image and Id of 152. However the query against the Email.CONTENT_URI gives me an id of 452 for the same contact (With same display name and email address). so when i try to get the Photo for a content uri containing the Id 452 it returns that the photo doesnt exist, but if i try to get the photo for 152 it works perfectly.

是什么原因造成这个问题?我如何得到正确的用户名?有没有办法,我也许可以运行来获得接触ID,或者一个正确的方式得到它与这一个的帮助任何关系查询。

What is causing this issue? how do i get the correct User ID? Is there any relational query that i can maybe run to get a contact ID, or maybe a correct way to get it with the help of this one.

感谢你。

修改

我发现周围的老code此挖掘。可能是有用的人。

I found this digging around old code. Might be helpful to anyone.

所以,完整的查询:

String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
            Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };

                String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
                        + " NOT LIKE '%@%' THEN 1" + " ELSE 2 END, "
                        + ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
                String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;

那么它的

getContentResolver()查询(Email.CONTENT_URI,投影,过滤器,空,订单);

推荐答案

当您要访问联系人的照片,你需要指定联系人的照片URI,例如使用此方法:

When you want to access the photo of a contact, you need to specify the contact photo URI, for example using this method:

public Uri getContactPhotoUri(long contactId) {
    Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    photoUri = Uri.withAppendedPath(photoUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    return photoUri;
}

但是,对于使用ContactID您必须使用:

But for contactId you must use:

String id = ContactsContract.CommonDataKinds.Photo.CONTACT_ID;
long contactId = Long.parseLong(id);

请注意,一个常见的​​错误是使用 ContactsContract.Contacts._ID ,而不是 ContactsContract.CommonDataKinds.Photo.CONTACT_ID

Please note that a common error is to use ContactsContract.Contacts._ID instead ContactsContract.CommonDataKinds.Photo.CONTACT_ID

我希望能帮助你。