I have been searching throughout Stackoverflow and the Android official documentation about retrieving the photos of Contacts, and I just don't understand it at all. I am pretty new to Java, so I don't even understand what an InputStream does or what a URI is, besides a "Uniform Resource Identifier."
Because of this, I just copy and pasted the code from the Android documentation because I figured there was no way to go wrong with it. Well it turns out, every single time I try to open a photo, it returns null. Google sure did make going about the contacts pretty dang hard. Like there is so much to simply retrieving a contact's name, let alone the picture.
Here is the code:
openPhoto()
function:
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;
}
Area where photo is opened:
...
InputStream stream = openPhoto(c.getID());
if(stream != null)
Log.i("PHOTO", stream.toString());
else
Log.i("NULL", "PHOTO IS NULL");
...
In the code above, the Logger always keeps logging "NULL" : "PHOTO IS NULL". So, Why is the photo of a contact not being found here?
EDIT: if you have an answer, please explain what is going on. I appreciate any answer, but I would like to learn what is going on. So far, this is still unsolved. So if you have an answer, just explain why. Thank you.