Android 从SQLite数据库检索图像时出现异常
以下是我所做的: 我创建了一个列(blob类型),并成功地将字节数组保存到其中。然后我通过这个代码得到了图像:Android 从SQLite数据库检索图像时出现异常,android,sqlite,android-contentprovider,illegalstateexception,Android,Sqlite,Android Contentprovider,Illegalstateexception,以下是我所做的: 我创建了一个列(blob类型),并成功地将字节数组保存到其中。然后我通过这个代码得到了图像: cursor.moveToFirst(); theme = cursor.getBlob(cursor.getColumnIndex("Theme")); bitmap = BitmapFactory.decodeByteArray(theme, 0, theme.length); screenBackGround.setBackground(new
cursor.moveToFirst();
theme = cursor.getBlob(cursor.getColumnIndex("Theme"));
bitmap = BitmapFactory.decodeByteArray(theme, 0, theme.length);
screenBackGround.setBackground(new BitmapDrawable(getResources(), bitmap));
- 注意:只有一行
- 我使用了一个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:无法从游标窗口读取第0行第1列。在从光标访问数据之前,请确保光标已正确初始化
我不知道为什么会出现这种异常,我想应该有OutOfMemory异常或其他异常,请向我解释并教我如何修复。谢谢大家! 您收到此消息是因为出于某种原因,您的光标包含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,否则它将指向第一条记录。在第二种情况下,图像是否正确添加到数据库?是的,它添加正确添加了您的查询,因为您没有得到任何结果检查行0