[英]Android Gallery view with images saved in sqlite as blob
我需要將圖像從sqlite數據庫顯示到gridview或gallery視圖中。
這是用於在單個視圖上顯示的代碼:
mMain = (ImageView) findViewById(R.id.ivMain);
byte[] blob = imgs.getBytes(); //there is a method that will return the bytes from the database
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
mMain.setImageBitmap(bitmap);
我有用於網格視圖的android教程,但它從文件獲取圖像
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3
... }
有沒有辦法從sqlite數據庫填充gridview?
更新:
我正在使用Android教程中提供的ImageAdapter:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
我的代碼變成這樣:
ArrayList<byte[]> image_arr = new ArrayList<byte[]>();
//loop through all images on sqlite
for(int l = 0 ;l< db.getAllImages().size(); l++){
Image imgs = db.getAllImages().get(l);
byte[] blob = imgs.getBytes();
image_arr.add(blob);
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// mMain.setImageBitmap(bitmap);
}
//TODO; find way to make the bitmap get to the ImageView
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
該代碼在主要活動上,因此我需要找到在其他文件中的ImageView中傳遞資源的方法。
private void getDataAndPopulate() {
image = new ArrayList<byte[]>();
caption = new ArrayList<String>();
cursor=db.rawQuery("select * from NAME",null);
while (cursor.moveToNext()) {
byte[] temp_image = cursor.getBlob(2);
String temp_caption = cursor.getString(1);
String temp_id= cursor.getString(0);
image.add(temp_image);
caption.add(temp_caption);
id.add(temp_id);
}
String[] captionArray = (String[]) caption.toArray(
new String[caption.size()]);
ItemsAdapter itemsAdapter = new ItemsAdapter(Item_show_grid.this, R.layout.item_grid,captionArray);
gv.setAdapter(itemsAdapter);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.