Java 如何在SQLite数据库中显示存储为BLOB的图像
我是android studio的新手,对此我有困难。我在android studio上的SQLite数据库中存储了一个图像:Java 如何在SQLite数据库中显示存储为BLOB的图像,java,android,sqlite,imageview,blob,Java,Android,Sqlite,Imageview,Blob,我是android studio的新手,对此我有困难。我在android studio上的SQLite数据库中存储了一个图像: public void onCreate(SQLiteDatabase db){ db.execSQL(create_table); } private static final String create_table = "create table if not exists Test ("+ "EntryID int
public void onCreate(SQLiteDatabase db){
db.execSQL(create_table);
}
private static final String create_table = "create table if not exists Test ("+
"EntryID integer primary key autoincrement, "+
"Description string,"+
"Picture blob"+
")";
我已将数据硬编码到数据库中,如下所示:
ContentValues cv5 = new ContentValues();
cv5.put("EntryID",1);
cv5.put("Description","The club was founded in 1885";
cv5.put("Picture","club.png");
sdb.insert("Test",null,cv5);
在另一个类中,我试图显示此存储的图像,但遇到了很多问题。我以前从未遇到过斑点
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Drawable myDrawable = getResources().getDrawable(R.drawable.club);
image.setImageDrawable(myDrawable);
}
当我试图在这个类中设置图像时,我得到了一个错误,应该是可绘制的。就像我说的,我对这个很陌生``
cv5.putPicture,club.png;//这是一种错误的方式
您需要先将图像转换为BLOB,诸如此类
ByteArrayOutputStream outStreamArray = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.common)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStrea);
byte[] photo = outStreamArray.toByteArray();cv5.put("Picture", photo)
之后,您需要将BLOB解码为图像
byte[] photo=cursor.getBlob(index of blob cloumn);
ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
Bitmap bitmap= BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(bitmap);
谢谢你,亚历克斯。你在哪里把水滴解码成图像?如果不获取非静态方法,我无法添加它。getBlobint无法从静态上下文引用