Android 将blob转换为位图后,图像未显示在imageView中
正如我在标题中提到的,我的图像不显示在imageView中。我已经包括了一个标题和描述以及图像,两者都在文本视图中正确显示Android 将blob转换为位图后,图像未显示在imageView中,android,Android,正如我在标题中提到的,我的图像不显示在imageView中。我已经包括了一个标题和描述以及图像,两者都在文本视图中正确显示 // RetrieveImage(); if(databaseExists()){ displayToast("Database exists"); db = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN
// RetrieveImage();
if(databaseExists()){
displayToast("Database exists");
db = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
if(db != null){
c = db.rawQuery("SELECT image,title,description FROM Images WHERE _Id =?",new String[]{"1"},null);
if (c != null ) {
if(c.moveToFirst()){
img = c.getBlob(1);
title = c.getString(1);
description = c.getString(1);
txt1.setText(title);
txt2.setText(title);
ByteArrayInputStream imageStream=ByteArrayInputStream(img);
Bitmap bm = BitmapFactory.decodeStream(imageStream);
imgView.setImageBitmap(bm);
}}}
我知道这将是一个简单的错误,但请帮助我发现这个错误
编辑:我附加了将图像插入数据库的代码段
yourSelectedImage = BitmapFactory.decodeFile(filePath);
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
img1.setImageBitmap(yourSelectedImage);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, bos);
img =bos.toByteArray();
private void handleDB() {
try {
String myPath = DB_PATH + DATABASE_NAME;
//open database in read and write mode
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
if(checkDB!=null){
ContentValues contentValues = new ContentValues();
contentValues.put("myid", myId );
contentValues.put("image", img);
contentValues.put("title", caption);
contentValues.put("description", description);
database.insert("Images", null,contentValues);
}
} catch(SQLiteException se ){
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
public void createDBFields(){
database = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
database.execSQL("CREATE TABLE IF NOT EXISTS Images ( _id INTEGER PRIMARY KEY, "+"myid nvarchar(50)," +"image BLOB," + "title nvarchar(50)," +
"description nvarchar(50));");
}//createDBFields close
可能您的img字节数组不是编码图像。这将无法正确显示标题和说明,您在c.getXXX中的索引都是相同的!如何插入图像?图像的大小是多少?@Michael,你说得对。它显示相同的值。但是,当我添加正确的索引时,例如img=c.getBlob3、title=c.getstring4或change img=c.getBlobc.getcolumnideximage,它就停止工作了。它对这些索引仍然有效吗?隐马尔可夫模型。。。您要添加的图像的大小是多少?大小在200 KB到3 MB之间