2012-02-20 125 views
1

我需要顯示圖像從sqlite數據庫到gridview或圖庫視圖。
這是顯示在一個單一的視圖代碼: Android圖庫視圖與保存在sqlite中的圖像blob

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?

UPDATE:
進出口使用的ImageAdapter在android系統教程提供:
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中的資源位於其他文件中。

回答

0
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); 

} 
+0

哇,那太快了!我會馬上檢查。感謝您的反饋 – 2012-02-20 07:40:33

+0

謝謝@aashutosh,ArrayList image = new ArrayList ();想法幫助我處理字節數組,這將傳遞給ItemsAdapter,但請解釋更多此ItemsAdapter我目前使用示例適配器:http://developer.android.com/resources/tutorials/views/hello-gridview html的 – 2012-02-20 08:37:51