在Android 2.3和Android 4.0上联系照片uri

时间:2012-08-31 07:54:59

标签: android uri android-contacts

我的代码有问题。我需要获取联系人照片uri,并在我的ImageView中显示图片。我编写了这段代码,它在Android 2.3中正常运行。

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

我的错误在哪里?

2 个答案:

答案 0 :(得分: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添加的。

答案 1 :(得分: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);

如果没有照片,它会抛出异常