Android ImageView无法访问某些联系人照片
我获得了联系人ID(Android ImageView无法访问某些联系人照片,android,exception,android-contacts,Android,Exception,Android Contacts,我获得了联系人ID(联系人Contacts.Contacts.\u ID) 我通过检查相应的Contacts Contacts.Contacts.photo_ID是否为null来确定照片是否可用 如果不是,我将为照片构建一个URI: Uri personUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,id); Uri photoUri=Uri.withAppendedPath(personUri, Co
联系人Contacts.Contacts.\u ID
)
我通过检查相应的Contacts Contacts.Contacts.photo_ID
是否为null
来确定照片是否可用
如果不是,我将为照片构建一个URI:
Uri personUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,id);
Uri photoUri=Uri.withAppendedPath(personUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
然后我使用setImageURI
方法将photoUri
设置为ImageView
对于一些照片,我看到了其他联系人的照片,我得到以下例外:
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)
我不知道为什么它对某些联系人不起作用
但我最想知道的是,为了避免这种异常,我应该测试什么?您可以使用
Contacts Contacts.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);
}