2010-02-15 28 views
5

Khi tôi lấy hình ảnh từ cơ sở dữ liệu sqlite đối tượng Bitmap của tôi bm trả về giá trị null có thể giúp ích cho tôi không ..?Android: vấn đề truy xuất bitmap từ cơ sở dữ liệu

tôi thấy vấn đề trong cơ sở dữ liệu của tôi ..

Khi tôi lưu trữ các mảng byte trong kiểu dữ liệu blob trong bảng cơ sở dữ liệu thời gian đó kích thước của mảng byte là 2280 ..

Nhưng khi tôi lấy ra rằng kiểu dữ liệu blob sử dụng chọn truy vấn tôi nhận được mảng byte trong kích thước 12.

mã của tôi là:

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

Cùng một vấn đề ở đây! Bạn đã vây giải pháp? –

Trả lời

10

đây là cách tôi mã hóa hình ảnh trước bằng văn bản cho dat làm mất thể diện:

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

Và sau đó giải mã nó như từ Cursor:

ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(columnIndex)); 
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream); 
Các vấn đề liên quan