Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageView无法访问某些联系人照片_Android_Exception_Android Contacts - Fatal编程技术网

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

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

我获得了联系人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, 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);  
}