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

查看:283
本文介绍了如何存储和检索一个字节数组(图像数据),并从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天全站免登陆