gpt4 book ai didi

android - 房间库不接受字节数组

转载 作者:搜寻专家 更新时间:2023-10-30 20:34:55 24 4
gpt4 key购买 nike

我正在使用适用于 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)

最佳答案

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

以下将达到目的。

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

关于android - 房间库不接受字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com