[英]Displaying image from the sqlite database using cursor in android
我想使用游標從sqlite數據庫中顯示imageview中的圖像。 我使用下面的代碼來檢索圖像,但我無法在imageview中顯示圖像。
Cursor c=this.db.query(TABLE_NAME, new String[] { "name","price","image" },
null, null, null, null, null);
name.setText(c.getString(0));
price.setText(c.getString(1));
byte b[]=c.getBlob(2);
Bitmap bp=BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image=(ImageView)findViewById(R.id.ImageView);
//ByteArrayInputStream imageStream = new ByteArrayInputStream(b);
//Bitmap theImage = BitmapFactory.decodeStream(imageStream);
//image.setImageBitmap(theImage);
image.setImageBitmap(bp);
除圖像外,將顯示名稱和價格,但不顯示圖像。 任何可以幫助我解決這個問題的建議或代碼。
請幫我。
提前致謝..
我試過使用Stream而不是字節數組,我的例子對我來說很有用。 也嘗試使用Cursor.getColumnIndex()
byte[] blob = c.getBlob(c.getColumnIndex(YourDB.IMAGE));
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
這是我的“創建表”聲明,請仔細檢查你的
create table datatable(_id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB, price INTEGER);
試試這個游標對象; //你的光標
mImageView.setImageBitmap(getImageFromBLOB(object.getBlob(object.getColumnIndex("book_thumb"))));
public static Bitmap getImageFromBLOB(byte[] mBlob)
{
byte[] bb = mBlob;
return BitmapFactory.decodeByteArray(bb, 0, bb.length);
}
首先請不要將圖像存儲在數據庫中,因為sqlite不支持超過1 mb,我可能錯了,圖像應該存儲在資源或drawble文件夾中,圖像名稱應該存儲在數據庫中然后你就可以得到它從代碼到代碼的資源轉換
String videoFileName= c.getString(c.getColumnIndex(TB_SETTING_VIDEOFILENAME));
int dot = videoFileName.lastIndexOf(".");
videoFileName=videoFileName.substring(0,dot);
Log.d("VIDEOFILENAME", videoFileName);
int videoFileRId = getResources().getIdentifier(videoFileName , "raw", getPackageName());
我希望這能幫到您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.