Store blobs in Android SQLite using SQLOpenHelper

Is there a way to save a blob in Android-SQLite using SQLOpenHelper?

My blob type InputStream.

+3
source share
1 answer

SQLite does not support BLOB or CLOB streaming. You have four options:

  • Convert InputStream to byte []. This will only work if you have enough memory (see below).
  • Use FileOutputStream or Android API that supports streaming
  • Split data into small blocks and save it in SQLite
  • , Android . H2. , Android H2 . API JDBC, , , LOB , BLOB .

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();
    }
}
+3

Source: https://habr.com/ru/post/1763183/


All Articles