获取Android中RawContact照片的URI

时间:2011-06-28 14:47:14

标签: android photos rawcontacts

我知道我可以使用以下方式获取联系人照片的URI:

Uri person = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);

有没有办法对RawContact做同样的事情?

我试过了:

Uri person = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
Uri photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);

但它不起作用......

我需要URI而不是实际blob的原因是因为代码在AppWidget中,并且在将数据从窗口小部件传递到启动器时似乎存在非常严格的限制,所以我需要使用setImageViewUri而不是setImageViewBitmap。

感谢。

4 个答案:

答案 0 :(得分:2)

以下是您的问题的解决方案。 Solution

我也有同样的问题,在挖掘并阅读Android的源代码后,我得到了它。现在可能已经很晚了(对你来说),但无论如何它都在这里。

public InputStream getBitmapFromUri(String rawContactId) throws FileNotFoundException {

    final Uri photoUri = Uri.withAppendedPath(
            ContentUris.withAppendedId(RawContacts.CONTENT_URI, Long.valueOf(rawContactId)), RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
    final InputStream imageStream = contentResolver.openInputStream(photoUri);

    return imageStream;
}

答案 1 :(得分:1)

我认为您需要直接访问“数据”表(ContactsContract.Data)以获取原始联系人的照片。列'data15'应该存储它。 Contacts.Photo.CONTENT_DIRECTORY是联系人的主要照片路径,但默认情况下没有为原始联系人提供此类路径。

答案 2 :(得分:0)

如果您要查询RawContacts,还可以在投影中添加RawContacts.CONTACT_ID,那么您可以采用相同的方式构建URI:

long contactId = cursor.getLong(cursor.getColumnIndex(RawContacts.CONTACT_ID));
Uri person = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(person, Contacts.Photo.CONTENT_DIRECTORY);

答案 3 :(得分:0)

一年后,没有真正的答案,我想这是无法做到的。

我最终使用setImageBitmap代替setImageUri, 并使用位图的缩放代码尽可能地减少内存消耗(请求越来越小的位图的循环,直到大小超出const限制)。

对于几乎所有的用户来说这似乎都很好用,有些人仍抱怨小部件中缺少图片,但我会说不到2-3%的用户是可以接受的。