gpt4 book ai didi

android cursor.getBlob 不工作

转载 作者:行者123 更新时间:2023-11-30 02:19:26 31 4
gpt4 key购买 nike

您好,我尝试在 sqlite 数据库中存储和检索图像。我的以下代码不起作用。我不确定我做错了什么。请帮忙。我创建的数据库表如下:

db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,
name VARCHAR,photo BLOB,marks VARCHAR);");

然后我插入字段

db.execSQL("INSERT INTO student VALUES('"
+ editRollno.getText() + "','"
+ editName.getText() + "','"
+ imageInByte + "','"
+ editMarks.getText() 方法
+ "');");

其中 imageInByte 是一个 byte[] 变量,之前从 gallery 中分配如下:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
imageInByte = stream.toByteArray();

当我尝试从数据库中检索图像时失败了:

Cursor c = db.rawQuery("SELECT * FROM student WHERE rollno='"
+ editRollno.getText() + "'", null);
if (c.moveToFirst()) {
editName.setText(c.getString(1));
editMarks.setText(c.getString(2));

byte[] image = c.getBlob(3);
ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
theImage = BitmapFactory.decodeStream(imageStream);
imageView2.setImageBitmap(theImage);

}

最佳答案

您不能简单地将字节数组视为文本。(要将 blob 与 execSQL 一起使用,您必须使用 blob 文字。)

要插入一行,请使用 insert method , 其中有 support for byte arrays .

关于android cursor.getBlob 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28811674/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com