Android 如何在数据库中保存位图?

Android 如何在数据库中保存位图?,android,Android,我想知道如何在数据库中保存位图。我在db中创建的表为: @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB)"); } 我的问题是无法插入图像。请告诉我需要插入图像的格式。将其写入二进制文件并存储为BLOB。。。你在正确的轨

我想知道如何在数据库中保存位图。我在db中创建的表为:

@Override
public void onCreate(SQLiteDatabase db)
{
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,image BLOB)");         
}    

我的问题是无法插入图像。请告诉我需要插入图像的格式。

将其写入二进制文件并存储为BLOB。。。你在正确的轨道上。您也可以对对象执行此操作

别忘了谷歌代码:


如果要这样做,需要使用blob。但是你为什么要这么做。。我不会将图像存储到数据库中。最好将图像存储在SD卡上,然后将其路径存储到数据库中。通常情况下,数据库安装在手机内存中,这只会填满手机内存

试试这个

InputStream is = mycon.getAssets().open("data/Images/img1");
                if (is.available() == -1) {
                    Log.v(null, "Images not read to Input stream");
                }
                if (is != null) {
                    BufferedInputStream bis = new BufferedInputStream(is, 128);
                    ByteArrayBuffer barb = new ByteArrayBuffer(128);
                    // read the bytes one by one and append it into the
                    // ByteArrayBuffer barb
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                        barb.append((byte) current);
                    }
                    values.put("imageData", barb.toByteArray());

如果您有位图图像,那么您可以执行以下操作

Bitmap photo = <Your image>
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();

我想知道你为什么要将它保存到数据库中。您可以将图像保存在某个位置,并将其路径存储在数据库中。+1除了不显示
创建表
代码之外,这应该被接受。建议不要将图像存储在数据库中,而是将其路径存储在db表中的文件系统中。我们是否应该将图像存储到用户手机内存或SD卡中?在sqlite中存储字节数组是否效率低下?
db = YourDBHelper.getInstance(ctx).getWritableDatabase();    
ContentValues values = new ContentValues();         
values.put("image", bArray);            
db.insert(TABLE_NAME , null, values);