【问题标题】:I get weird exception when retrieve image from SQLite database从 SQLite 数据库中检索图像时出现奇怪的异常
【发布时间】:2015-12-24 15:35:11
【问题描述】:

这是我所做的:

我创建了一个列(blob 类型)并成功地将字节数组保存到其中。然后我通过这段代码得到了图像:

    cursor.moveToFirst();
    theme = cursor.getBlob(cursor.getColumnIndex("Theme"));  
    bitmap = BitmapFactory.decodeByteArray(theme, 0, theme.length);
    screenBackGround.setBackground(new BitmapDrawable(getResources(), bitmap));
  • 注意:只有 1 行。

第一次尝试:

  • 我使用了一张 500x500 (12.6KB) 的图片,效果很好。

然后第二次尝试:

  • 我使用了 768x1280 (120KB) 的图像,然后我收到了这个错误:

    12-24 06:24:54.965:E/CursorWindow(1186):无法从具有 0 行 3 列的 CursorWindow 读取第 0 行第 1 列。

    12-24 06:24:54.965: E/AndroidRuntime(1186): java.lang.IllegalStateException: 无法从 CursorWindow 读取第 0 行第 1 列。确保 Cursor 在从中访问数据之前已正确初始化。

我不知道为什么我会得到这个异常,我认为应该有 OutOfMemory 异常或某事,请给我解释并教我如何解决这个问题。谢谢!

【问题讨论】:

  • 在第二种情况下,图像是否正确添加到数据库中??
  • 是的,它已正确添加
  • 添加您的查询,因为您没有得到任何检查行 0

标签: android sqlite android-contentprovider illegalstateexception


【解决方案1】:

您收到此消息是因为出于某种原因您的光标包含 0 行。 像这样修改你的代码:

  if(cursor.moveToFirst())
    {
    theme = cursor.getBlob(cursor.getColumnIndex("Theme"));  
    bitmap = BitmapFactory.decodeByteArray(theme, 0, theme.length);
    screenBackGround.setBackground(new BitmapDrawable(getResources(), bitmap));
    }

如果返回 0 行,cursor.moveToFirst() 将返回 false,否则将指向第一条记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多