[英]Display all images from sqlite database
I recently made a post asking how to display blobs from a sqlite database but got no replies. 我最近发表了一篇文章,询问如何显示来自sqlite数据库的blob,但没有得到答复。 After more painstaking hours :( I've finally found an answer that does display the blobs in an image view, however it only displays the last blob in the database. 经过多小时的苦苦挣扎:(我终于找到了在图像视图中确实显示blob的答案,但是它只显示数据库中的最后一个blob。
Is there something wrong with my query/loop? 我的查询/循环有问题吗? or am I totally wrong? 还是我完全错了?
Activity Class that displays the blobs 显示Blob的活动类
public class championsActivity extends Activity {
private Bitmap champions;
private myDBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_champions);
ImageView imageView = (ImageView)findViewById(R.id.champ_splash);
db= new myDBHelper(this);
champions = db.getChamp_splash();
imageView.setImageBitmap(champions);
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
}
}
DBHelper class DBHelper类
public class myDBHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "champions.db";
private static final int DATABASE_VERSION = 2;
public myDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
setForcedUpgrade();
}
public Bitmap getChamp_splash() {
Bitmap bitmap = null;
SQLiteDatabase db = getReadableDatabase();
db.beginTransaction();
String sqlTables = "Champions";
String selectQuery = "SELECT * FROM "+ sqlTables;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
while (c.moveToNext()) {
byte[] blob = c.getBlob(c.getColumnIndex("champ_splash"));
bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
}
}
db.setTransactionSuccessful();
db.endTransaction();
return bitmap;
}
}
EDIT 编辑
After going over the answers given I have now added the blobs to an array however I still cannot display them. 经过给出的答案后,我现在已将斑点添加到数组中,但是仍然无法显示它们。 This is my current code 这是我当前的代码
public ArrayList<Bitmap> getChamp_splash() {
Bitmap bitmap = null;
SQLiteDatabase db = getReadableDatabase();
db.beginTransaction();
String sqlTables = "Champions";
String selectQuery = "SELECT * FROM "+ sqlTables;
Cursor c = db.rawQuery(selectQuery, null);
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
if (c != null) {
while (c.moveToNext()) {
byte[] blob = c.getBlob(c.getColumnIndex("champ_splash"));
bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
bitmaps.add(bitmap);
}
}
db.setTransactionSuccessful();
db.endTransaction();
return bitmaps;
}
public class championsActivity extends Activity {
private GridView gridView;
private Cursor champions;
private myDBHelper db;
private ArrayList<Bitmap> champ_splash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_champions);
ImageView imageView = (ImageView)findViewById(R.id.champ_splash);
GridView gridView = (GridView)findViewById(R.id.champion_grid);
db= new myDBHelper(this);
champ_splash = db.getChamp_splash();
imageView.setImageBitmap(champ_splash);
champions = db.getChampions();
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
champions,
new String[] {"Name"},
new int[] {android.R.id.text1});
gridView.setAdapter(adapter);
}
At first glance, this is what you programmed in your code - return last image. 乍一看,这就是您在代码中编写的内容-返回最后一张图片。 Code with some comments: 带有一些注释的代码:
// create bitmap
Bitmap bitmap = null;
SQLiteDatabase db = getReadableDatabase();
db.beginTransaction();
String sqlTables = "Champions";
String selectQuery = "SELECT * FROM "+ sqlTables;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
// go through the result
while (c.moveToNext()) {
byte[] blob = c.getBlob(c.getColumnIndex("champ_splash"));
// assign each value to bitmap - not very useful as on next iteration - previous bitmap will be overriden
// on last iteration - set to bitmap last item - all previous will be deleted
bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
}
}
db.setTransactionSuccessful();
db.endTransaction();
return bitmap;
If you want to retrieve collection of objects - return collection of objects. 如果要检索对象集合-返回对象集合。
What you should be doing is storing bitmaps into array, like this: 您应该做的是将位图存储到数组中,如下所示:
public ArrayList<Bitmap> getChamp_splash() {
...
...
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
if (c != null) {
while (c.moveToNext()) {
byte[] blob = c.getBlob(c.getColumnIndex("champ_splash"));
bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
bitmaps.add(bitmap);
}
}
...
...
return bitmaps;
}
and then in your championsActivity
: 然后在您的championsActivity
:
private ArrayList<Bitmap> champions;
private GridView imageGridview;
...
...
imageGridview = (GridView) findViewById(R.id.my_grid_view);
champions = db.getChamp_splash();
if (champions != null) { // if the bitmaps were found
ImageGridViewAdapter adapter = new ImageGridViewAdapter(this, bitmaps, columnWidth); // using custom adapter for showing images
// columnWidth is just some int value representing image width
imageGridview.setAdapter(adapter);
}
and the ImageGridViewAdapter
: 和ImageGridViewAdapter
:
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageGridViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Bitmap> bitmaps;
private int imageWidth;
public ImageGridViewAdapter (Context c, ArrayList<Bitmap> bitm, int width) {
this.mContext = c;
this.bitmaps = bitm;
this.imageWidth = width;
}
public int getCount()
{
return bitmaps.size();
}
public Object getItem(int position) {
return bitmaps.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(mContext);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
imageView.setImageBitmap(bitmaps.get(position));
return imageView;
}
}
Of course, this is displaying images into a GridView
, if you want to display images differently, then you have to change the adapter. 当然,这是将图像显示到GridView
,如果您希望以不同的方式显示图像,则必须更改适配器。
PS: I currently cannot test this if it's working perfectly, but this is how I displayed an array of bitmaps in a grid in one of my previous projects. PS:目前我无法测试它是否工作正常,但这是我在以前的项目之一中的网格中显示位图数组的方式。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.