8

I am using Room library for Android to make a database. It has one simple table with id, date, title, text, address and image.
The problem appears when I try to insert byte array into the table (I am using test entries through for loop for test). I am converting the image to byte[]. It works ok when I put simple string instead of image.

Here is the error:

Failed to read row 0, column 0 from a CursorWindow which has 0 rows, 6 columns.

FATAL EXCEPTION: AsyncTask #1
Process: com.database.test.app, PID: 18552
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetLong(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:524)
at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
at com.database.test.app.EntryDao_Impl.getAllEntries(EntryDao_Impl.java:183)
at com.database.test.app.MainActivity$CreateAndExportBase.doInBackground(MainActivity.java:191)
at com.database.test.app.MainActivity$CreateAndExportBase.doInBackground(MainActivity.java:151)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
ligi
  • 39,001
  • 44
  • 144
  • 244
filipst
  • 1,547
  • 1
  • 30
  • 55
  • 1
    Why you try to save binary file into database? – DeKaNszn Jul 19 '17 at 08:27
  • I have to insert an image into the db. – filipst Jul 19 '17 at 08:30
  • 1
    You should not use database for storing files. Put them into application data dir and save path in database. – DeKaNszn Jul 19 '17 at 08:32
  • 1
    I have to insert them into db because I am exporting db as a backup file. There will be many images. – filipst Jul 19 '17 at 08:33
  • Create zip with images and database file. Read (for example) https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – DeKaNszn Jul 19 '17 at 08:36
  • Problem is that I have limited storage size for backup https://developer.android.com/guide/topics/data/backup.html – filipst Jul 19 '17 at 08:44
  • If your images cached from web, then don't backup it. If images are user-generated content - you may backup it to backend/storage. User data backup is not for storing images. – DeKaNszn Jul 19 '17 at 08:50
  • So it is not available to convert user-generated images to byte[], insert them into DB and backup the DB? Only way is to backup them to some storage? – filipst Jul 19 '17 at 08:52
  • You may convert byte[] to string (for example via base64), but your database file size will be more than size of all images. And it may be greater than allowed to backup – DeKaNszn Jul 19 '17 at 09:03
  • 2
    cursor window has a system dependent limit (usually 2mb) and your images might be going over that limit. – yigit Jul 24 '17 at 06:05
  • did you find the solution? @filipst – RushDroid Dec 10 '17 at 15:49
  • @RushDroid unfortunately no, I didn't. – filipst Dec 11 '17 at 07:44
  • Possible duplicate of [How insert image in room persistence library?](https://stackoverflow.com/questions/46337519/how-insert-image-in-room-persistence-library) – Nimrod Dayan Jul 25 '18 at 06:43

1 Answers1

11

To store image file into db you need to declare your table field with "BLOB" datatype. But Room will consider "byte[]" as "String" and thats y you are not able to store image.

Following will do the trick.

@ColumnInfo(name = "your columnname",typeAffinity = ColumnInfo.BLOB)
private byte[] yourfield;
Pinakin Kansara
  • 2,273
  • 3
  • 22
  • 35
  • @RushDroid setting typeAffinity to Blob works for me and others https://stackoverflow.com/questions/46337519/how-insert-image-in-room-persistence-library/46356934#46356934. Can you please post the error you are getting? – Pinakin Kansara Dec 11 '17 at 06:12
  • can you please share your code if you don't mind.unfortunately its not working for me @Pinakin – RushDroid Dec 11 '17 at 06:14
  • See the answer posted in the link contains example code for byte declaration.https://stackoverflow.com/questions/46337519/how-insert-image-in-room-persistence-library/46356934#46356934 – Pinakin Kansara Dec 11 '17 at 06:15
  • Then share your example implementation so, I can debug and let you know what is wrong. or share error you are getting. – Pinakin Kansara Dec 11 '17 at 06:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160900/discussion-between-rushdroid-and-pinakin). – RushDroid Dec 11 '17 at 06:17
  • How bytearray converted to blob? How we can re-convert this blob data to bytearray in other platforms? – Ali Zarei Sep 17 '22 at 10:24