我需要将 sqlite 数据库中的图像显示到 gridview 或 gallery View 中。
这是在单个 View 上显示的代码:
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);
我有 GridView 的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));
此代码位于主要 Activity 中,因此我需要找到方法来传递其他文件中的 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);
}
关于java - Android 图库 View ,其中图像保存在 sqlite 中作为 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9357448/