2014-04-01 19 views
0

我讀取drawable並將其保存到數據庫。此外,我從數據庫中讀取,但得到空。 你能幫我嗎?decodeByteArray返回null

這裏是我寫的數據庫:

BitmapDrawable drawable = (BitmapDrawable) imageResult.getDrawable(); 
Bitmap data = drawable.getBitmap(); 

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
data.compress(CompressFormat.JPEG, 100, stream); 

db = dbHelp.getWritableDatabase(); 
ContentValues cv = new ContentValues(); 
cv.put("data", stream.toByteArray()); 

db.insert("table", null, cv); 

這是我如何從數據庫中讀取:

db = dbHelp.getReadableDatabase(); 
String getProjects = "select data from table where id = (select max(id) from table);"; 
Cursor cu = db.rawQuery(getProjects, null); 
cu.moveToFirst(); 
data = BitmapFactory.decodeByteArray(cu.getBlob(0), 0, cu.getBlob(0).length); //here is null 
+0

返回1行 – user3082303

回答

0
public Bitmap getImage(){ 

    String qu = "select data from table where id = (select max(id) from table)" ; 
    Cursor cur = db.rawQuery(qu, null); 

    if (cur.moveToFirst()){ 
     byte[] imgByte = cur.getBlob(0); 
     cur.close(); 
     return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); 
    } 
    if (cur != null && !cur.isClosed()) { 
     cur.close(); 
    }  

    return null ; 
} 

編輯:

BitmapDrawable drawable = (BitmapDrawable) imageResult.getDrawable(); 
Bitmap data = drawable.getBitmap(); 

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
data.compress(CompressFormat.JPEG, 60, stream); 

db = dbHelp.getWritableDatabase(); 
ContentValues cv = new ContentValues(); 
cv.put("data", stream.toByteArray()); 

db.insert("table", null, cv); 
+0

謝謝你的回覆。我得到錯誤窗口已滿。請求分配在行字節[] imgByte = cur.getBlob(0); – user3082303

+0

okk我的回答我爲你工作? –

+0

我得到錯誤窗口已滿。請求分配在行字節[] imgByte = cur.getBlob(0); – user3082303