2013-03-19 60 views
1

我有一個代碼在我的應用程序獲取一個聯繫人的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

更新:我看到它只發生在沒有被設備拍攝的照片上(比如從谷歌帳戶收到的照片或從互聯網上下載的照片)。

回答

3

改變了獲取照片輸入流的方式後,所有的工作。

相反的:

InputStream isPhoto = context.getContentResolver().openInputStream(photoUri); 

我用:

InputStream photoInputStream = 
    Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri); 

關注:現在開放的是接觸而不是他的照片的。

0

雖然從ContactsContract.Contacts.Photo下面的例子中,我得到了同樣的異常,因爲你沒有嘗試使用openDisplayPhoto時:

public InputStream openDisplayPhoto(long contactId) { 
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 
      contactId); 
    Uri displayPhotoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo 
      .DISPLAY_PHOTO); 
    try { 
     AssetFileDescriptor fd = 
       getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r"); 
     return fd.createInputStream(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

而且它使用openPhoto時工作得很好(這實際上是縮略圖):

public InputStream openPhoto(long contactId) { 
    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 
      contactId); 
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo 
      .CONTENT_DIRECTORY); 
    Cursor cursor = getContentResolver().query(photoUri, 
      new String[]{ContactsContract.Contacts.Photo.PHOTO}, null, null, null); 
    if (cursor == null) { 
     return null; 
    } 
    try { 
     if (cursor.moveToFirst()) { 
      byte[] data = cursor.getBlob(0); 
      if (data != null) { 
       return new ByteArrayInputStream(data); 
      } 
     } 
    } finally { 
     cursor.close(); 
    } 
    return null; 
} 

事實上,當聯繫人圖片來自Google+時,它只會以縮略圖的形式提供,而不是正常的全尺寸圖片。