正如我在标题中提到的,我的图像不会显示在imageView中。我已经包含了标题和描述以及图像,并且都在textViews中正确显示。
// 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