从ContactsContract Android显示照片

时间:2014-02-26 08:22:39

标签: android android-cursoradapter

我正在使用下面的CursorAdapter来显示联系人,它正在运行。 但我似乎无法显示照片。

首先我发送了PHOTO_THUMBNAIL_URI并使用了pic.setImageURI。但这只适用于2-3张照片而这些照片有时会被重复使用..至少可以说是一些奇怪的行为。

在网上浏览我真的找不到有效的溶剂。 下面的溶剂不是弹性CRASH我的应用程序,但它也没有显示任何图片。

我尝试同时提供PHOTO_THUMBNAIL_URI,PHOTO_URI,_ID和PHOTO_FILE_ID但没有成功。

我做错了什么? :(

public static class CustomAdapter extends SimpleCursorAdapter {

    private int mSelectedPosition;
    private Context context;
    private int layout;

    @Override
    public View newView(final Context context, Cursor cursor, ViewGroup parent) {

        final Cursor c = getCursor();

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        TextView name_text = (TextView) v.findViewById(R.id.KontakterNamn);
        ImageView pic = (ImageView) v.findViewById(R.id.RealKontakter_bild);

        Bitmap photo = null;

        Uri displayPhotoUri = ContentUris.withAppendedId(ContactsContract.DisplayPhoto.CONTENT_URI, c.getColumnIndex("PHOTO_THUMBNAIL_URI"));
        try {
            AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(
                    displayPhotoUri, "r");
            photo = BitmapFactory.decodeStream(fd.createInputStream());
            pic.setImageBitmap(photo);
        } catch (IOException e) {
            // do nada
        }

        int nameCol = c.getColumnIndex("DISPLAY_NAME");
        final String name = c.getString(nameCol);

        name_text.setText(name);

        return v;
    }


    public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.layout = layout;
    }


    @Override
    public void bindView(View v, final Context context, Cursor c) {

        TextView name_text = (TextView) v.findViewById(R.id.KontakterNamn);
        ImageView pic = (ImageView) v.findViewById(R.id.RealKontakter_bild);

        Bitmap photo = null;

        Uri displayPhotoUri = ContentUris.withAppendedId(ContactsContract.DisplayPhoto.CONTENT_URI, c.getColumnIndex("PHOTO_THUMBNAIL_URI"));
        try {
            AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(
                    displayPhotoUri, "r");
            photo = BitmapFactory.decodeStream(fd.createInputStream());
            pic.setImageBitmap(photo);
        } catch (IOException e) {
            //
        }

        int nameCol = c.getColumnIndex("DISPLAY_NAME");
        final String name = c.getString(nameCol);

        name_text.setText(name);

        int position = c.getPosition();

    }


    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
        notifyDataSetChanged();

    }
}
static CustomAdapter mCursorAdapter;

1 个答案:

答案 0 :(得分:0)

尝试以下

byte[] imageInByte;
String imageuri = null;

            imageuri = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

            if (imageuri == null) {
                Bitmap image = BitmapFactory.decodeResource(getResources(),
                        R.drawable.ic_launcher);

                // convert bitmap to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                imageInByte = stream.toByteArray();

            } else {

                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                            this.getContentResolver(), Uri.parse(imageuri));

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    imageInByte = stream.toByteArray();

                } catch (Exception e) {
                    e.getMessage();

                }

            }

之后在图像视图中设置该字节

byte[] mbitmap = imageInByte;
            Bitmap bitmap = BitmapFactory.decodeByteArray(mbitmap, 0, mbitmap.length);

            ((ImageView)view).setImageBitmap(bitmap);