我的代碼有問題。我需要獲取聯繫人照片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
哪裏是我的錯?
你找到答案它將拋出一個異常?如果你發現它PLZ分享它,因爲IAM面臨同樣的問題 – AnasBakez