Java 如何在Sqlite DB表中保存图像

Java 如何在Sqlite DB表中保存图像,java,android,Java,Android,我必须将相机拍摄的图像保存在SQLITE DB中?有人能帮我吗?我是Android新手。您可以将图像保存为BLOB格式 或者。您可以使用Base64对其进行转换,并将其保存为CLOB格式 退房 我同意天威的观点,将图像保存到Sqlite是个坏主意。您应该将其路径保存到sqlite中。与文本信息相比,图像的大小更大 从sqlite保存和检索图像是很困难的 因此,我建议您将图像存储在外部文件夹中,并将其路径存储到sqlite数据库中我们总是将图像的url保存在db中 但是,您也可以获取图像的字节并在

我必须将相机拍摄的图像保存在SQLITE DB中?有人能帮我吗?我是Android新手。

您可以将图像保存为
BLOB
格式

或者。您可以使用Base64对其进行转换,并将其保存为
CLOB
格式

退房

我同意天威的观点,将图像保存到Sqlite是个坏主意。您应该将其路径保存到sqlite中。与文本信息相比,图像的大小更大

从sqlite保存和检索图像是很困难的


因此,我建议您将图像存储在外部文件夹中,并将其路径存储到sqlite数据库中

我们总是将图像的url保存在db中

但是,您也可以获取图像的字节并在db中插入数据(该列是Blob)。

使用“Blob”存储图像。 您需要将该图像存储在字节数组中

要存储图像,可以使用以下代码:

public void insertImg(int id , Bitmap img ) {   


    byte[] data = getBitmapAsByteArray(img);    
    insertStatement_logo.bindLong(1, id);       
    insertStatement_logo.bindBlob(2, data);

    insertStatement_logo.executeInsert();
    insertStatement_logo.clearBindings() ; 
}

 public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}

然后创建图像的位图

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

dba.open();

dba.insertPhoto(byteArray);
其中dba是数据库类的对象

在数据库类中创建表,如:

private static final String CREATETABLE_PHOTO = "create table eqpphoto("EImage BLOB " + ");";

public static final String TABLE_PHOTO = "eqpphoto";

public long insertPhoto(byte[] EImage) {

        try {
            System.out.println("Function call : ");
            ContentValues values = new ContentValues();

            values.put(EIMAGE, EImage);
            return db.insert(TABLE_PHOTO, null, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
检查