samedi 21 mars 2015

Display all images from sqlite database

I recently made a post asking how to display blobs from a sqlite database but got no replies. 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.


Is there something wrong with my query/loop? or am I totally wrong?


Activity Class that displays the blobs



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

Aucun commentaire:

Enregistrer un commentaire