开发者

Android ImageView unable to access some contact photos

开发者 https://www.devze.com 2023-03-10 00:19 出处:网络
I get the contact ID (ContactsContract.Contacts._ID) I determine if a photo is available by checking if the corrisponding ContactsContract.Contacts.PHOTO_ID is null.

I get the contact ID (ContactsContract.Contacts._ID)

I determine if a photo is available by checking if the corrisponding ContactsContract.Contacts.PHOTO_ID is null.

If it's not I build a URI to the photo:

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

Then I set the photoUri to an ImageView using its setImageURI method.

For some photos I see the 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 is not working for some contacts?

But mostly I'd like do know what should I test for in order to avoid this exception?


You can use ContactsContract.Contacts.openContactPhotoInputStream(Context, Uri) method to check if the photo associated with a contact is readable or not.

E.g.

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);  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号