2012-02-20 120 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