有没有一种方法可以使用 SQLOpenHelper
将 BLOB 存储到 Android 的 SQLite 中?
我的 BLOB 类型为 InputStream
。
有没有一种方法可以使用 SQLOpenHelper
将 BLOB 存储到 Android 的 SQLite 中?
我的 BLOB 类型为 InputStream
。
SQLite不支持流式传输BLOB或CLOB数据。你有四个选项:
要将InputStream转换为字节数组,可以使用以下代码:
public static byte[] readBytesAndClose(InputStream in) throws IOException {
try {
int block = 4 * 1024;
ByteArrayOutputStream out = new ByteArrayOutputStream(block);
byte[] buff = new byte[block];
while (true) {
int len = in.read(buff, 0, block);
if (len < 0) {
break;
}
out.write(buff, 0, len);
}
return out.toByteArray();
} finally {
in.close();
}
}