大型BLOB从android中的SQLiteDatabase获取

时间:2014-07-22 12:30:21

标签: android image bitmap crop

在这里,我目前正在开展一个项目,包括从Gallery或Camera& amp;在数据库中存储后裁剪此图像。这很完美。但是当图像大小很大时,它会完美地存储在数据库中,但是当从数据库中获取此图像时会出现错误。

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
    }

那么,如何储存&从数据库中检索大尺寸图像?

1 个答案:

答案 0 :(得分:2)

您需要检查cursor.moveToFirst(),然后才能访问Cursor Data.like

 Cursor cursor = database.query(Const.TABLE_NAME1, null, null, null, null, null, null);

do{
  if(cursor.moveToFirst()) {

  //Do your job

 }
}while(cursor.moveToNext());

 cursor.close();