2010-02-15 12 views
5

Quando recupero l'immagine dal database sqlite il mio oggetto Bitmap bm restituisce un valore nullo qualcuno può aiutarmi ..?Android: problema nel recupero della bitmap dal database

ho trovato problema nel mio database ..

Quando devo conservare l'array di byte nel tipo di dati blob nella tabella del database che il tempo la dimensione della matrice di byte erano 2280 ..

Ma quando ho recuperato che tipo di dati blob utilizzando query di selezione ottengo l'array di byte nel formato 12.

il mio codice è:

// 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()); 
} 
+0

Stesso problema qui! Hai una soluzione? –

risposta

10

Ecco come ho codificare l'immagine prima di scrivere alla dat abase:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
mBitmap.compress(Bitmap.CompressFormat.JPEG, THUMB_QUALITY, outputStream); 
mByteArray = outputStream.toByteArray(); // write this to database as blob 

E poi decodificarlo come il cursore da:

ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(columnIndex)); 
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream); 
Problemi correlati