0

I've got a problem. I can't get and set blob from my database. After open my app I get this error in my Logcat

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

private static final String KEY_IMAGE = "image";

    public Note getNote(long id){
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.query(DATABASE_TABLE,new String[] {KEY_ID,KEY_TITLE,KEY_CONTENT,KEY_PHONE,KEY_CLIENT,KEY_AGE,KEY_DATE,KEY_TIME,KEY_IMAGE}, KEY_ID+"=?",

                    new String[]{String.valueOf(id)}, null, null,null);
            if (cursor != null)
                cursor.moveToFirst();
            return new Note(cursor.getLong(0)
                    ,cursor.getString(1)
                    ,cursor.getString(2)
                    ,cursor.getString(3)
                    ,cursor.getString(4)
                    ,cursor.getString(5)
                    ,cursor.getString(6)
                    ,cursor.getString(7)
            ,cursor.getBlob(8));
        }

        public List<Note> getNotes() {
            SQLiteDatabase db = this.getReadableDatabase();
            List<Note> allNotes = new ArrayList<>();

            String query = "SELECT * FROM " + DATABASE_TABLE + " ORDER BY "+KEY_TITLE+" ASC";
            Cursor cursor = db.rawQuery(query,null);
            if(cursor.moveToFirst()){
                do{
                    Note note = new Note();
                    note.setID(Long.parseLong(cursor.getString(0)));
                    note.setTitle(cursor.getString(1));
                    note.setAge(cursor.getString(2));
                    note.setPhone(cursor.getString(3));
                    note.setClient(cursor.getString(4));
                    note.setContent(cursor.getString(5));
                    note.setDate(cursor.getString(6));
                    note.setTime(cursor.getString(7));
                    note.setImage(cursor.getBlob(8));
                    allNotes.add(note);


                }while(cursor.moveToNext());

            }

            return allNotes;

        }

Thanks for help.

mufazmi
  • 1,103
  • 4
  • 18
  • 37
Olek
  • 11
  • 4

1 Answers1

0

I think you're not using the correct method for the data your're accessing in the database, for example your'r saying getLong(0) but may be there is some other type of value in that column , please double check your create table query, and if you want to store images you should use a fileprovider instead of blob.

Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
  • It doesn't help ;/ In public Note getNote cursor KEY_IMAGE is implemented as new String, I dont know how to write it in byte[], have a look – Olek Mar 01 '20 at 13:25
  • can you please share your subclass of SQLiteOpenHelper – Abhinav Chauhan Mar 01 '20 at 13:32
  • i am asking for the class where you defined the database tables – Abhinav Chauhan Mar 02 '20 at 05:30
  • i am not seeing the class where you created the database – Abhinav Chauhan Mar 02 '20 at 08:53
  • https://anotepad.com/notes/c483dixb now you got it ?"Zdjęcie" - image – Olek Mar 02 '20 at 10:01
  • you are using file provider as data type for the image i don't know if it can be used like that , the correct way of using a file provider is given here : https://developer.android.com/reference/android/support/v4/content/FileProvider . – Abhinav Chauhan Mar 02 '20 at 10:47
  • Have you any contact to someone who can help me with this ? – Olek Mar 02 '20 at 14:07
  • you can't write fileprovider as you're writing in your table defination , you can learn how to use it in the link that i provided , and use getInt() for getting the id. – Abhinav Chauhan Mar 02 '20 at 14:46
  • you don't put put images directly in the database that is why i am saying read the documentation for the file provider using it will be less messy and fast, but if you want to images in the db read this -> https://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database – Abhinav Chauhan Mar 02 '20 at 14:55