2012-08-31 73 views
4

我的代碼有問題。我需要獲取聯繫人照片URI,並在我的ImageView中顯示圖片。我寫了這段代碼,它在Android 2.3中正常工作。在Android 2.3和Android 4.0上聯繫人照片uri

Uri myUri = getPhotoUriById(this, contactId); 
(ImageView)myImageView.setImageUri(myUri); 

public static Uri getPhotoUriById(Context context, Long id) { 
    if(id == null || context == null) return null; 

    Uri photo = Uri.parse("android.resource://"+ context.getPackageName() +"/" + R.drawable.no_photo); 

    Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, 
      new String[]{ Contacts.PHOTO_ID }, 
      Contacts._ID + " = " + id + " AND " + Contacts.PHOTO_ID + " != 0", 
      null, 
      null); 
    if(cursor != null) { 
     if(cursor.moveToFirst()) { 
      photo = ContentUris.withAppendedId(Contacts.CONTENT_URI, id); 
      photo = Uri.withAppendedPath(photo, Contacts.Photo.CONTENT_DIRECTORY); 
     } 
     cursor.close(); 
    } 
    return photo; 
} 

但是當我在Android 4.0(ICS)測試它。我遇到了很多問題。

08-31 14:32:02.059: W/System.err(616): java.io.IOException: read failed: EINVAL (Invalid argument) 
08-31 14:32:02.069: W/System.err(616): at libcore.io.IoBridge.read(IoBridge.java:432) 
08-31 14:32:02.069: W/System.err(616): at java.io.FileInputStream.read(FileInputStream.java:179) 
08-31 14:32:02.079: W/System.err(616): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168) 
08-31 14:32:02.079: W/System.err(616): at java.io.BufferedInputStream.read(BufferedInputStream.java:309) 
08-31 14:32:02.079: W/System.err(616): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
08-31 14:32:02.079: W/System.err(616): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493) 
08-31 14:32:02.079: W/System.err(616): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351) 
08-31 14:32:02.079: W/System.err(616): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773) 
08-31 14:32:02.089: W/System.err(616): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:733) 
08-31 14:32:02.089: W/System.err(616): at android.widget.SuggestionsAdapter.getDrawable(SuggestionsAdapter.java:577) 
08-31 14:32:02.099: W/System.err(616): at android.widget.SuggestionsAdapter.getDrawableFromResourceValue(SuggestionsAdapter.java:543) 
08-31 14:32:02.099: W/System.err(616): at android.widget.SuggestionsAdapter.getIcon1(SuggestionsAdapter.java:399) 
08-31 14:32:02.099: W/System.err(616): at android.widget.SuggestionsAdapter.bindView(SuggestionsAdapter.java:345) 
08-31 14:32:02.099: W/System.err(616): at android.widget.CursorAdapter.getView(CursorAdapter.java:250) 
08-31 14:32:02.109: W/System.err(616): at android.widget.SuggestionsAdapter.getView(SuggestionsAdapter.java:483) 
08-31 14:32:02.109: W/System.err(616): at android.widget.AbsListView.obtainView(AbsListView.java:2033) 
08-31 14:32:02.109: W/System.err(616): at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1168) 
08-31 14:32:02.109: W/System.err(616): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 
08-31 14:32:02.119: W/System.err(616): at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1095) 
08-31 14:32:02.119: W/System.err(616): at android.widget.ListPopupWindow.show(ListPopupWindow.java:524) 
08-31 14:32:02.119: W/System.err(616): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1062) 
08-31 14:32:02.119: W/System.err(616): at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:939) 
08-31 14:32:02.119: W/System.err(616): at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:921) 
08-31 14:32:02.119: W/System.err(616): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285) 
08-31 14:32:02.130: W/System.err(616): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-31 14:32:02.139: W/System.err(616): at android.os.Looper.loop(Looper.java:137) 
08-31 14:32:02.139: W/System.err(616): at android.app.ActivityThread.main(ActivityThread.java:4340) 
08-31 14:32:02.139: W/System.err(616): at java.lang.reflect.Method.invokeNative(Native Method) 
08-31 14:32:02.139: W/System.err(616): at java.lang.reflect.Method.invoke(Method.java:511) 
08-31 14:32:02.150: W/System.err(616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
08-31 14:32:02.150: W/System.err(616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
08-31 14:32:02.150: W/System.err(616): at dalvik.system.NativeStart.main(Native Method) 
08-31 14:32:02.159: W/System.err(616): Caused by: libcore.io.ErrnoException: read failed: EINVAL (Invalid argument) 
08-31 14:32:02.169: W/System.err(616): at libcore.io.Posix.readBytes(Native Method) 
08-31 14:32:02.169: W/System.err(616): at libcore.io.Posix.read(Posix.java:118) 
08-31 14:32:02.179: W/System.err(616): at libcore.io.BlockGuardOs.read(BlockGuardOs.java:137) 
08-31 14:32:02.179: W/System.err(616): at libcore.io.IoBridge.read(IoBridge.java:422) 
08-31 14:32:02.179: W/System.err(616): ... 31 more 
08-31 14:32:02.189: D/skia(616): ---- read threw an exception 

哪裏是我的錯?

+0

你找到答案它將拋出一個異常?如果你發現它PLZ分享它,因爲IAM面臨同樣的問題 – AnasBakez

回答

5

幾天後,我寫了正確的代碼。這適用於所有Android版本。 有我的解決方案

public static Uri getPhotoUriById(Context context, Long id) { 
    if(id == null || context == null) return null; 

    Uri photo = Uri.parse("android.resource://"+ context.getPackageName() +"/" + R.drawable.no_photo); 

    try { 
     Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, 
       new String[]{ Contacts.PHOTO_ID, "photo_uri" }, 
       Contacts._ID + " = " + id + " AND " + Contacts.PHOTO_ID + " != 0", 
       null, 
       null); 
     if(cursor == null) 
      return photo; 
     if(!cursor.moveToFirst()) { 
      cursor.close(); 
      return photo; 
     } 
     String sUri; 
     if((sUri = cursor.getString(cursor.getColumnIndex("photo_uri"))) != null) { 
      cursor.close();               // content://com.android.contacts/display_photo/1 
      return Uri.parse(sUri); 
     } 
     cursor.close(); 
     return photo; 
    } catch(Exception e) { 
     Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, 
       new String[]{ Contacts.PHOTO_ID }, 
       Contacts._ID + " = " + id + " AND " + Contacts.PHOTO_ID + " != 0", 
       null, 
       null); 
     if(cursor == null) 
      return photo; 
     if(!cursor.moveToFirst()) { 
      cursor.close(); 
      return photo; 
     } 
     cursor.close(); 
     photo = ContentUris.withAppendedId(Contacts.CONTENT_URI, id); 
     photo = Uri.withAppendedPath(photo, Contacts.Photo.CONTENT_DIRECTORY); 
     return photo; 
    } 
} 

「photo_uri」是一個常數Contacts.PHOTO_URI,這是從API級別11

+0

嗨,我有這個代碼時,我使用的問題,它給了我在TRY正文的ICS Android版本的不良URI。任何解決方案或我錯過了什麼? –

+0

正如你所說的「photo_uri」是從API LEVEL 11中添加的,那麼它將如何在Android應用程序中使用支持2.2?我得到了一個像** content://com.android.contacts/contacts/271/photo**這樣的同步聯繫人圖片的不同URI,因此它不會提供任何圖片作爲回報,並且會按照上面的說明提供異常。 –

+0

@RushabhPatel catch()塊在API 10及更低版本的設備上工作,因爲getContentResolver()。query()會給出異常,在這種情況下,該函數返回即** content://com.android.contacts/contacts/251 /照片**。 – artemkaxboy

1

原來的答案效果很好,但我認爲這可以提高大量補充。我沒有看到try/catch中的需求,當你可以使用底部的代碼時,它沒有折舊的方法。這個支持API的級別爲10 - 19(可能低於此值,但我從未檢查過)。這是我得到它下降到

Uri photo = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); 
    photo = Uri.withAppendedPath(photo, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(contentResolver, photo); 

如果沒有照片