Android从数据中获取联系人照片。电子邮件查询

发布于 2024-10-22 20:38:03 字数 1722 浏览 3 评论 0原文

我正在制作一个自动完成字段,通过显示名称和电子邮件查询联系人。当某人在过滤后单击所需的联系人时,该联系人将被添加到包含其电子邮件、显示姓名和图片(如果有)的列表中。

到目前为止,除了让照片出现之外,我已经完成了所有事情。以下是我运行查询来获取电子邮件、显示名称、ID 和照片 ID 的方法。

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

其中投影是:

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

这个执行我需要的操作并返回所有数据。但我在调试此问题期间注意到的一件事是,联系人 ID 与针对特定显示名称针对 ContactsContract.Contacts.CONTENT_URI 运行查询不同。

例如,我运行的测试通过运行 Contacts.CONTENT_URI 获取所有联系人,给了我一个图像和 ID 为 152 的联系人。但是,针对 Email.CONTENT_URI 的查询为我提供了同一联系人的 id 452 (使用相同的显示名称和电子邮件地址)。因此,当我尝试获取包含 Id 452 的内容 uri 的照片时,它会返回该照片不存在,但如果我尝试获取 152 的照片,它会完美运行。

是什么导致了这个问题?我如何获得正确的用户 ID?是否有任何我可以运行来获取联系人 ID 的关系查询,或者可能是在这个查询的帮助下获取它的正确方法。

谢谢。

编辑

我发现这是在挖掘旧代码。可能对任何人都有帮助。

所以完整的查询:

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().query( Email.CONTENT_URI, PROJECTION, filter, null, order);

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

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.

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.

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.

Thank you.

EDIT

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

So the full query:

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;

then its

getContentResolver().query( Email.CONTENT_URI, PROJECTION, filter, null, order);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小巷里的女流氓 2024-10-29 20:38:03

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

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 您必须使用:

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

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

我希望这可以帮助您。

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

But for contactId you must use:

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

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

I hope that can help you.

南薇 2024-10-29 20:38:03

您应该在查询中使用 RAW_CONTACT_ID。例如,可以有两个不同的联系人,即单个 CONTACT_ID 具有不同的 RAW_CONTACT_ID。

You should use RAW_CONTACT_ID in the query. For ex, there can be two different contacts i.e. different RAW_CONTACT_ID for a single CONTACT_ID.

醉态萌生 2024-10-29 20:38:03

也许你可以看看这篇博客文章中的示例,他们查询所有联系人、电子邮件地址和联系人照片
http:// blog.app-solut.com/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/

maybe you can take a look at this blog post in the example there they query all contacts, email addresses and the contact photo
http://blog.app-solut.com/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/

岁月蹉跎了容颜 2024-10-29 20:38:03

最好的代码将适用

Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Id);

    Bitmap photoBitmap;
    ContentResolver cr = getContentResolver();
    InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);

    photoBitmap = BitmapFactory.decodeStream(is);

于所有人

best code will be

Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Id);

    Bitmap photoBitmap;
    ContentResolver cr = getContentResolver();
    InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);

    photoBitmap = BitmapFactory.decodeStream(is);

it works for all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文