【发布时间】:2026-01-25 09:10:01
【问题描述】:
我一直在 * 和 Android 官方文档中搜索有关检索联系人照片的信息,但我完全不明白。我对 Java 很陌生,所以除了“统一资源标识符”之外,我什至不了解 InputStream 的作用或 URI 是什么。
因此,我只是复制并粘贴了 Android 文档中的代码,因为我认为它不会出错。事实证明,每次我尝试打开照片时,它都会返回 null。谷歌确实让联系人变得非常困难。就像简单地检索联系人姓名一样,更不用说图片了。
代码如下:
openPhoto()函数:
private InputStream openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri,
new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null);
if(cursor == null) {
return null;
}
try {
if(cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
if(data != null)
return new ByteArrayInputStream(data);
}
} finally {
cursor.close();
}
return null;
}
打开照片的区域:
...
InputStream stream = openPhoto(c.getID());
if(stream != null)
Log.i("PHOTO", stream.toString());
else
Log.i("NULL", "PHOTO IS NULL");
...
在上面的代码中,Logger 始终保持记录“NULL”:“PHOTO IS NULL”。那么,为什么这里找不到联系人的照片?
编辑:如果您有答案,请解释发生了什么。我很感激任何答案,但我想了解发生了什么。到目前为止,这仍然没有解决。因此,如果您有答案,请解释原因。谢谢。
【问题讨论】:
标签: java android android-contacts