2014-02-17 34 views
2

我正在嘗試檢索原始聯繫人的照片。我能成功獲得高分辨率照片給定的原料接觸,但是當我想要得到的縮略圖照片相同的原始接觸,我得到這個異常:獲取Raw聯繫人縮略圖照片

02-17 05:43:44.695: E/DatabaseUtils(4071): Writing exception to parcel 
02-17 05:43:44.695: E/DatabaseUtils(4071): java.lang.IllegalArgumentException: URI: content://com.android.contacts/raw_contacts/8/photo, calling user: com.pedro.notesquirrel, calling package:com.pedro.notesquirrel 
02-17 05:43:44.695: E/DatabaseUtils(4071): at com.android.providers.contacts.LegacyApiSupport.query(LegacyApiSupport.java:1914) 
02-17 05:43:44.695: E/DatabaseUtils(4071): at com.android.providers.contacts.ContactsProvider2.queryLocal(ContactsProvider2.java:6378) 
02-17 05:43:44.695: E/DatabaseUtils(4071): at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:4999) 
02-17 05:43:44.695: E/DatabaseUtils(4071): at android.content.ContentProvider$Transport.query(ContentProvider.java:200) 
02-17 05:43:44.695: E/DatabaseUtils(4071): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 
02-17 05:43:44.695: E/DatabaseUtils(4071): at android.os.Binder.execTransact(Binder.java:404) 
02-17 05:43:44.695: E/DatabaseUtils(4071): at dalvik.system.NativeStart.run(Native Method) 

我使用Restlet框架,但我不」我認爲這與這個問題有什麼關係。

這裏是我的代碼:

這裏mHighResolutionBoolean,當它是false它會生成異常。當它是真的時,它顯示照片。 所以,

mHighResolution == false -> exception 
mHighResolution == true -> works fine 

public InputStream getPhotoInputStream() { 
     Uri uri = Uri.withAppendedPath(ContactsContract.RawContacts.CONTENT_URI, String.valueOf(mRawContactId)); 
     return ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri, mHighResolution); 
} 

@Override 
    public void handle(Request request, Response response) { 

     String type = request.getMethod().getName(); 
     String uid = (String) request.getAttributes().get("uid"); 

     if(type.equalsIgnoreCase("get")) 
     { 
      try { 
       Representation r = processGet(uid); 
       response.setEntity(r); 
      } catch (NotFoundException e) { 
       Log.e(TAG, e.getMessage(), e); 
       response.setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage())); 
      } catch (IOException e) { 
       Log.e(TAG, e.getMessage(), e); 
       response.setStatus(new Status(Status.SERVER_ERROR_INTERNAL, e.getMessage()));   
      } 
     } 

private Representation processGet(String uid) throws NotFoundException, IOException 
{ 
    Photo photo = new Photo(mContext, uid); 
    Representation representation = new InputRepresentation(photo.getPhotoInputStream()); 

    return representation; 
} 

回答

2

的照片縮略圖從Contacts數據庫保存在Data表。

Android的保存照片無論是在圖像文件(高分辨率),或在數據庫中作爲一個blob

要訪問你可以使用我使用的問題的方法的圖像文件。要訪問數據庫中的縮略圖,您可以使用此代碼:

public InputStream getPhotoThumbnailInputStream(String uid) 
{ 
    final String[] projection = new String[]{Data.DATA15}; 
    final String selection = Data.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + " =?"; 
    final String[] selectionArgs = new String[]{uid, android.provider.ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE}; 

    final Cursor cursor = contentResolver.query(Data.CONTENT_URI, projection, selection, selectionArgs, null); 

    if(cursor.moveToFirst()) 
    { 
     byte[] photo = cursor.getBlob(0); 
     InputStream is = new ByteArrayInputStream(photo); 
     cursor.close(); 
     return is; 
    } 
    else { 
     Log.e(TAG, "Photo thumbnail not found for the given raw contact id."); 
     cursor.close(); 
     return null; 
    } 
}