Android: Problem beim Abrufen der Bitmap aus der Datenbank
Wenn ich ein Bild aus der SQLite-Datenbank abrufe, wird mein Bitmap-Objekt angezeigtbm
Rückgabewert null kann mir jemand helfen ..?
Ich habe ein Problem in meiner Datenbank gefunden.
Als ich das Bytearray im Blob-Datentyp in der Datenbanktabelle speicherte, betrug die Größe des Bytearrays zu dieser Zeit 2280.
Aber wenn ich diesen Blob-Datentyp mit Auswahlabfrage abgerufen habe, erhalte ich das Byte-Array in Größe 12.
Mein Code ist:
// Inserting data in database
byte[] b;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
b = baos.toByteArray();
//here b size is 2280
baos.close();
try
{
mDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
mDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (PICTURE BLOB);");
mDB.execSQL("INSERT INTO "
+ MY_DATABASE_TABLE
+ " (PICTURE)"
+ " VALUES ('"+b+"');");
}
catch(Exception e)
{
Log.e("Error", "Error", e);
}
finally
{
if(mDB != null)
mDB.close();
}
// Retriving data from database
byte[] b1;
Bitmap bm;
mDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
try {
mDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ MY_DATABASE_TABLE
+ " (PICTURE BLOB);");
Cursor c = mDB.rawQuery("SELECT * FROM " + MY_DATABASE_TABLE + ";", null);
c.moveToFirst();
if (c != null) {
do {
b1=c.getBlob(0)); //here b1 size is 12
bm=BitmapFactory.decodeByteArray(b1, 0, b1.length);
}while(c.moveToNext());
}