显示sqlite数据库中的所有图像

时间:2015-03-21 21:25:40

标签: java android sqlite bitmap blob

我最近发了一篇文章,询问如何从sqlite数据库中显示blob但没有得到回复。经过更多艰苦的工作:(我终于找到了一个在图像视图中显示blob的答案,但它只显示数据库中的最后一个blob。

我的查询/循环有问题吗?或者我完全错了?

显示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类

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);
}

2 个答案:

答案 0 :(得分:0)

乍一看,这是您在代码中编写的内容 - 返回最后一张图像。代码中有一些评论:

// 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;

如果要检索对象集合 - 返回对象集合。

答案 1 :(得分:0)

您应该做的是将位图存储到数组中,如下所示:

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:如果它工作正常,我目前无法测试它,但这是我在之前的一个项目中在网格中显示位图数组的方式。

希望这有帮助。