如何在 SQLite 数据库中存储和检索字节数组(图像数据)? [英] How to store and retrieve a byte array (image data) to and from a SQLite database?

查看:47
本文介绍了如何在 SQLite 数据库中存储和检索字节数组(图像数据)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Android 中的 SQLite 数据库中存储和检索字节数组(图像数据)?

How do I store and retrieve a byte array (image data) to and from a SQLite database in Android?

推荐答案

字节数组可以存储为 BLOB 数据类型.除了特别注意分块外,此过程没有什么特别之处,例如:

Byte array can be stored as BLOB data type. There's nothing special in this procedure except special care for chunking, like:

InputStream is = context.getResources().open(R.drawable.MyImageFile);
try {
    byte[] buffer = new byte[CHUNK_SIZE];
    int size = CHUNK_SIZE;
    while(size == CHUNK_SIZE) {
        size = is.read(buffer);  //read chunks from file
        if (size == -1) break;

        ContentValues cv = new ContentValues();
        cv.put(CHUNK, buffer);       //CHUNK blob type field of your table

        long rawId = database.insert(TABLE, null, cv); //TABLE table name
    }
} catch (Exception e) {
    Log.e(TAG, "Error saving raw image to: "+rawId, e);
}

这篇关于如何在 SQLite 数据库中存储和检索字节数组(图像数据)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆