【发布时间】:2014-09-13 06:42:31
【问题描述】:
在这里,我目前正在从事一个项目,其中包括从图库或相机获取图像并在存储到数据库后裁剪此图像。这是完美的工作。但是当图像大小很大时,它可以完美地存储在数据库中,但是从数据库中获取此图像时会出现错误。
java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
将位图转换为字节数组
public byte[] bitmpToByte(Bitmap bmp)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imgarray = stream.toByteArray();
return imgarray;
}
将此字节数组存储在数据库中。
values.put("blob_image", imgArray); // byte[] imgArray;
从数据库中获取并转换为位图并显示。
Record helper = new Record getActivity());
Database database = helper.getReadableDatabase();
Cursor cursor = database.query(Const.TABLE_NAME1, null, null, null, null, null, null);
if(cursor.getCount() > 0 && cursor != null) {
cursor.moveToFirst();
id = String.valueOf(cursor.getString(0));
com_name.setText(cursor.getString(1));
byte[] imgArray= cursor.getBlob(2);
Bitmap bitmap = BitmapFactory.decodeByteArray(imgArray , 0, imgArray.length);
displayImage.setImageBitmap(bitmap);
}
try {
cursor.close();
helper.close();
} catch (Exception e) {
// TODO: handle exception
}
那么,如何从数据库中存储和检索大尺寸图像?
【问题讨论】:
-
没有问题是关于你的
Cursor不是Image.. 检查你的光标并发布更多代码。 -
@MD 请检查此代码。
-
@JS 检查它
if(cursor.moveToFirst())并从光标中获取值 -
如果moveToFirst()返回true,则尝试访问数据,否则不要
-
我尝试过,但仍然出现此错误。 @MD