安卓ImageView无法访问某些联系人照片

3

我获取联系人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)

我不确定为什么对于某些联系人无法工作?

但是我想知道我应该测试什么以避免出现此异常?


有人可以帮忙吗?!?!? - Asher
1个回答

1

您可以使用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);  
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接