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)
A byte array is simply an area of memory containing a group of contiguous (side by side) bytes, such that it makes sense to talk about them in order: the first byte, the second byte etc..
Byte arrays mostly contain binary data such as an image. If the byte array that you are trying to convert to String contains binary data, then none of the text encodings (UTF_8 etc.) will work.
Why these classes use byte array instead of character array ? Before very few Strings are about words taken from a spoken language. They're almost all computer code, using exclusively ASCII characters, that can be coded in 7-bits. Using 16-bits or more than that per character feels very wasteful on memory.
Whereas bytearray methods that change the sequence actually change the sequence. You would prefer using bytearray , if you are editing a large object (e.g. an image's pixel buffer) through its binary representation and you want the modifications to be done in-place for efficiency.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With