2010-02-15 8 views
5

sqlite 데이터베이스에서 이미지를 가져올 때 비트 맵 객체 bm 반환 값이 null 일 수 있습니까?안드로이드 : 데이터베이스에서 비트 맵을 가져 오는 중 문제가 발생했습니다.

내 데이터베이스에서 문제가 발견

..

나는 데이터베이스 테이블에 BLOB 데이터 유형의 바이트 배열을 바이트 배열의 크기가 2280이었다 그 시간을 저장

..

을하지만 난 것을 검색 할 때

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

같은 문제! 해결 방법이 있습니까? –

답변

10

내가 나타나서에 쓰기 전에 이미지를 인코딩하는 방법은 다음과 같습니다 선택 쿼리를 사용하여 BLOB 데이터 유형은 내가 12

내 코드는 크기 내에서 바이트 배열을 얻을 베이스상의 :

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

그리고이 커서에서처럼 디코딩 : 여기

ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(columnIndex)); 
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream); 
관련 문제