我有一個代碼在我的應用程序獲取一個聯繫人的URI,然後通過嘗試打開照片的輸入流,獲取照片的URI和解碼來驗證聯繫人是否有照片它。例外沒有找到的照片文件ID 0
很少,我得到一個奇怪的例外,該URI不存在。我得到一個不存在的照片文件的URI。
獲取照片URI:
// Get Uri to the contact
Uri contact = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
c.getInt(c.getColumnIndex(PhoneLookup._ID)));
// Get input stream of the photo
InputStream is = Contacts.openContactPhotoInputStream(context.getContentResolver(), contact);
// If no input stream (then there is no photo of contact), return 'null' instead of photo Uri
if (is == null) {
Log.d(TAG, "No photo");
return;
}
// Get display photo Uri of the contact
Uri photoUri = Uri.withAppendedPath(contact, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
此時,當聯繫人沒有照片,is
得到null
和回報。但很少,photoUri
得到content://com.android.contacts/contacts/303/display_photo
。
現在對其進行解碼(在其他功能):
InputStream isPhoto = context.getContentResolver().openInputStream(photoUri);
是從該行引發的異常是:
java.io.FileNotFoundException: No photo file found for ID 0
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:617)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:717)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:614)
at android.content.ContentResolver.openInputStream(ContentResolver.java:449)
at com.app.android.utils.ImageUtils.decodeBitmapFromPhotoUri(ImageUtils.java:39)
...
這怎麼可能是內容解析器給我的URI一張不存在的照片(聯繫人存在並且沒有照片,肯定是),而不是返回null
?
更新:我看到它只發生在沒有被設備拍攝的照片上(比如從谷歌帳戶收到的照片或從互聯網上下載的照片)。