Android ImageView无法访问某些联系人照片
我获取联系人 ID (ContactsContract.Contacts._ID
)
我通过检查相应的 ContactsContract.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)
我不确定为什么它对某些联系人不起作用?
但最重要的是我想知道我应该测试什么以避免这种异常?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ContactsContract.Contacts.openContactPhotoInputStream(Context, Uri)
方法检查与联系人关联的照片是否可读。例如
You can use
ContactsContract.Contacts.openContactPhotoInputStream(Context, Uri)
method to check if the photo associated with a contact is readable or not.E.g.