房间图书馆不接受字节数组

时间:2017-07-19 08:06:01

标签: android arrays database sqlite android-room

我正在使用Android的Room库来创建数据库。它有一个简单的表,包括id,日期,标题,文本,地址和图像 当我尝试将字节数组插入表中时出现问题(我通过for循环进行测试使用测试条目)。我正在将图片转换为byte[]。当我放简单的字符串而不是图像时,它工作正常。

这是错误:

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) 

1 个答案:

答案 0 :(得分:1)

要将映像文件存储到db,您需要使用“BLOB”数据类型声明表字段。 但是Room会将“byte []”视为“String”,那就是你无法存储图像。

以下将做到这一点。

@ColumnInfo(name = "your columnname",typeAffinity = ColumnInfo.BLOB)
private byte[] yourfield;