显示来自 sqlite 数据库的所有图像
Display all images from sqlite database
我最近 post 询问如何显示来自 sqlite 数据库的 blob,但没有得到回复。经过更多的艰苦努力:(我终于找到了一个确实在图像视图中显示 blob 的答案,但是它只显示数据库中的最后一个 blob。
我的query/loop有问题吗?还是我完全错了?
Activity Class 显示 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
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;
}
}
编辑
在检查了给出的答案后,我现在已经将 blob 添加到数组中,但是我仍然无法显示它们。这是我当前的代码
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);
}
乍一看,这就是您在代码中编写的内容 - return 最后一张图片。带有一些注释的代码:
// 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;
如果要检索对象集合 - return 对象集合。
你应该做的是将位图存储到数组中,像这样:
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;
}
然后在你的 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);
}
和 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;
}
}
当然,这是把图片显示成一个GridView
,如果你想显示不同的图片,那你就得换适配器了。
P.S:我目前无法测试它是否运行良好,但这是我在之前的一个项目中在网格中显示位图数组的方式。
希望对您有所帮助。
我最近 post 询问如何显示来自 sqlite 数据库的 blob,但没有得到回复。经过更多的艰苦努力:(我终于找到了一个确实在图像视图中显示 blob 的答案,但是它只显示数据库中的最后一个 blob。
我的query/loop有问题吗?还是我完全错了?
Activity Class 显示 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
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;
}
}
编辑
在检查了给出的答案后,我现在已经将 blob 添加到数组中,但是我仍然无法显示它们。这是我当前的代码
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);
}
乍一看,这就是您在代码中编写的内容 - return 最后一张图片。带有一些注释的代码:
// 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;
如果要检索对象集合 - return 对象集合。
你应该做的是将位图存储到数组中,像这样:
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;
}
然后在你的 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);
}
和 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;
}
}
当然,这是把图片显示成一个GridView
,如果你想显示不同的图片,那你就得换适配器了。
P.S:我目前无法测试它是否运行良好,但这是我在之前的一个项目中在网格中显示位图数组的方式。
希望对您有所帮助。