【发布时间】:2015-05-25 03:26:21
【问题描述】:
我最近发表了一篇文章,询问如何显示 sqlite 数据库中的 blob,但没有得到回复。经过更艰苦的几个小时:(我终于找到了一个在图像视图中显示 blob 的答案,但是它只显示数据库中的最后一个 blob。
我的查询/循环有问题吗?还是我完全错了?
显示 blob 的 Activity 类
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 类
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);
}
【问题讨论】:
标签: java android sqlite bitmap blob