1
嗨我试图存储和检索图像和sqlite数据库。我的下面的代码不起作用。我不确定我做了什么错误。请帮忙。 我创建的数据库表如下:android cursor.getBlob不工作
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是其如下从库之前分配一个字节[]变量:
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);
}
尝试编写'editRollno.getText()。toString()'而不是'editRollno.getText()',并为所有其他人做。 – Apurva 2015-03-02 14:07:37