所以我一直在搜索整個Stackoverflow和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");
...
在上面的代碼中,記錄器總是保持登錄「NULL 「:」PHOTO IS NULL「。所以,爲什麼在這裏找不到聯繫人的照片?
編輯: 如果您有答案,請解釋發生了什麼。我很欣賞任何答案,但我想知道發生了什麼。到目前爲止,這仍然沒有解決。所以,如果你有答案,只要解釋原因。謝謝。