2

Here, I am currently working in a project which is include Image getting from Gallery or Camera & Crop this image after store in database. It's work perfectly. But When image size is large then it's store in database perfectly but when getting this image from database it's give s error.

java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 

Convert Bitmap to byte Array

public byte[] bitmpToByte(Bitmap bmp)
 {
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
     byte[] imgarray = stream.toByteArray();
     return  imgarray;
 }

Store this byte array in database.

values.put("blob_image", imgArray); // byte[] imgArray;

Getting from database & convert to bitmap & display.

    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
    }

So, How to store & retrieve large size image from database?

12
  • No issue is about your Cursor not for Image.. check your Cursor and post more code.
    – M D
    Jul 22, 2014 at 12:34
  • @MD Please Check this code. Jul 22, 2014 at 12:39
  • @JS check it if(cursor.moveToFirst()) and fetch value from cursor
    – M D
    Jul 22, 2014 at 12:44
  • 1
    if moveToFirst() return true, then try to access the data, otherwise don't
    – elmorabea
    Jul 22, 2014 at 12:46
  • i try with but still this error getting. @MD Jul 22, 2014 at 12:48

1 Answer 1

2

You need to check cursor.moveToFirst() then only you access the 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();
2
  • java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. this error getting Jul 22, 2014 at 12:56
  • @JigarShekh try with .rawQuery(....) like Cursor cursor = db.rawQuery("SELECT * FROM " + Const.TABLE_NAME1, null); and you make sure that your TABLE_NAME1 is not NULL
    – M D
    Jul 22, 2014 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.