Android ImageView cannot access some contact photos

I get contact id ( ContactsContract.Contacts._ID)

I determine if the photo is available by checking if the value matches ContactsContract.Contacts.PHOTO_ID null.

If it’s not, I create a URI for the photo:

Uri personUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,id);
Uri photoUri=Uri.withAppendedPath(personUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);    

Then I installed photoUriin ImageViewusing his method setImageURI.

For some photos, I see a picture for other contacts, I get the following exception:

Unable to open content: content://com.android.contacts/contacts/1912/photo
java.io.FileNotFoundException: java.io.FileNotFoundException: No results.
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:123)
    at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:538)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:484)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:319)
    at android.widget.ImageView.resolveUri(ImageView.java:521)
    at android.widget.ImageView.setImageURI(ImageView.java:305)

I'm not sure why it does not work for some contacts?

But basically, I would like to know what should I check to avoid this exception?

+3
source share
1 answer

ContactsContract.Contacts.openContactPhotoInputStream(Context, Uri), , , , .

.

InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(getContext().getContentResolver(), personUri);

if (is == null) {
 // Your contact doesn't have a valid photo 
 // i.e. use the default photo for your app
} else {
 // This will always succeed when assigned to an ImageView!
 return Uri photoUri=Uri.withAppendedPath(personUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);  
}
+1

All Articles