【问题标题】:Unsupported operation exception when trying to getBlob from cursor尝试从游标获取 Blob 时出现不支持的操作异常
【发布时间】:2013-05-23 07:10:01
【问题描述】:

这是我的 SimpleCursorAdapter 扩展类,我使用它来尝试在 ListView 中显示有关联系人的信息:

private class CustomContactsAdapter extends SimpleCursorAdapter {
        private int layout;
        private LayoutInflater inflater;

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

        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View v = inflater.inflate(layout, parent, false);
            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor cur) {
            MatrixCursor c = (MatrixCursor) cur;
            final String name = c.getString(c.getColumnIndex(COLUMN_NAME));
            final String org  = c.getString(c.getColumnIndex(COLUMN_ORG));
            final byte[] image = c.getBlob(c.getColumnIndex(COLUMN_PHOTO));

            ImageView iv = (ImageView) v.findViewById(R.id.contact_photo);

            if(image != null && image.length > 3) {
                iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0, image.length));
            }

            TextView tname = (TextView) v.findViewById(android.R.id.text1);
            tname.setText(name);

            TextView torg = (TextView) v.findViewById(android.R.id.text2);
            torg.setText(org);
        }
    }

但是当程序到达我想从游标中获取 blob 数据的代码 sn-p 时,会抛出 UnsupportedOperationException 并显示消息:

不支持getBlob

我想知道我做错了什么。另外,我将自己烘焙的MatrixCursor 作为参数传递给适配器。

【问题讨论】:

  • 哪个android版本抛出异常?
  • 安卓4.2.2版本
  • 您的设备装有 Android 4.2.2 或者您正在针对 Android 4.2.2 进行编译?

标签: android listview simplecursoradapter android-cursoradapter android-cursor


【解决方案1】:

这是getBlob(int) 在Android 1.6 中的MatrixCurosr 和Android 2.3 的实现。

public byte[] getBlob(int column) {
    throw new UnsupportedOperationException("getBlob is not supported");
}

这是 Android 的 getBlob(int) 实现 ICS

 @Override
 public byte[] getBlob(int column) {
        Object value = get(column);
        return (byte[]) value;
 }

您可能想继承 MatrixCursor 并以 ICS 方式实现 getBlob

【讨论】:

  • 感谢您提到不同版本的方法,会知道的。但正如我所说,我使用 Android 4.2.2 并在 2.3 模拟器上运行这个应用程序。
  • 你在哪里运行它很重要(我的句子有道理)
  • 哦,我迟到了,您已将 Android 2.3 添加到您的答案中,看来我真的必须覆盖 MatrixCursor 逻辑
  • > Object value = get(column); 不幸的是 get(int) 方法仅在 ICS 中提供,我应该在 2.3 中做什么?
  • 我也不喜欢它,但如果您需要在 ICS 之前使用的东西,它可能是一个有效的解决方法。把它作为最后的手段。在您的情况下,您可以将图像存储为 Base64 字符串,而不是获取 blob 数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-13
  • 2020-02-14
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
相关资源
最近更新 更多