我在检索图像时遇到的另一个问题 (Blob) 将显示检索图像,如果我将图像从数据库记录更改为另一个图像,则之前的图像将是现金,不会更改为新图像。 .! 数据库表:
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS student(" +
"Sid INTEGER PRIMARY KEY AUTOINCREMENT, " +
"firstName TEXT, " +
"lastName TEXT, " +
"photo BLOB, " +
"book TEXT)";
db.execSQL(sql);
}
手动插入一些数据
ContentValues values = new ContentValues();
values.put("firstName", "Roza");
values.put("lastName", "Jack");
// profile picture of sudent
values.put("photo", convertimg(R.drawable.sudent_1));
values.put("book", "Andriod programming");
db.insert("student", "book", values);
将方法图像转换为 BLOB
public static byte[] convertimg(int drawimg)
{
Bitmap bitmap = BitmapFactory.decodeResource(myresurse, drawimg);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100 , bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
并检索图像并设置为 imageview。
db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cur=db.rawQuery("select firstname || ' ' || lastname as fullname,book,photo from student where Sid=1" , null);
cur.moveToNext();
ImageView imgprofile = (ImageView) findViewById(R.id.studentphoto);
byte[] mybyte=cur.getBlob(cur.getColumnIndex("photo"));
ByteArrayInputStream imageStream = new ByteArrayInputStream(mybyte);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
imgprofile.setImageBitmap(theImage);
imgprofile.invalidate();
如果我想更改以前的图像,我的大问题就在这里
values.put("photo", convertimg(R.drawable.sudent_1));
像这样的图片,第二张图片不会显示,而是第一张图片
values.put("photo", convertimg(R.drawable.sudent_2));
最佳答案
尝试在设置 imgprofile.setImageBitmap(theImage);
后调用 imgprofile.invalidate();
。这基本上会重绘 ImageView。
关于从 SQLite 数据库中检索时,Android Image 不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21849650/