安卓:问题检索数据库,从位图 [英] Android: problem retrieving bitmap from database

查看:119
本文介绍了安卓:问题检索数据库,从位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从SQLite数据库检索图像我的位图对象 BM 返回空值,任何一个可以帮助我..?

我发现问题在我的数据库。

当我在BLOB数据类型的字节数组存储在数据库表的时间字节数组的大小为2280 ..

但是,当我使用select查询检索到的BLOB数据类型,我得到大小12中的字节数组。

我的code是:

  //数据库中插入数据
字节[] B:
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
位图BM = BitmapFactory.de codeResource(getResources(),R.drawable.icon);
bm.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS); // BM是位图对象
B = baos.toByteArray();
//这里B规格为2280
baos.close();
尝试
{
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
+(资料图片)
+VALUES('+ B +'););}
赶上(例外五)
{
Log.e(错误,错误,E);
}最后
{
如果(MDB!= NULL)
mDB.close();
}
从数据库// Retriving数据
字节[] B1;
位图BM;
MDB = this.openOrCreateDatabase(MY_DATABASE_NAME,MODE_PRIVATE,NULL);
尝试{
mDB.execSQL(CREATE TABLE IF NOT EXISTS
+ MY_DATABASE_TABLE
+(PICTURE BLOB););光标C = mDB.rawQuery(SELECT * FROM+ MY_DATABASE_TABLE +;,NULL);
c.moveToFirst();
如果(C!= NULL){
做{
B1 = c.getBlob(0)); //这里B1尺寸为12
BM = BitmapFactory.de codeByteArray的(B1,0,b1.length);
}而(c.moveToNext());
}


解决方案

下面是如何我带code写入数据库之前的图像:

  ByteArrayOutputStream的OutputStream =新ByteArrayOutputStream();
mBitmap.com preSS(Bitmap.Com pressFormat.JPEG,THUMB_QUALITY,OutputStream的);
mByteArray = outputStream.toByteArray(); //写这个数据库为BLOB

然后去code它就像从光标:

  ByteArrayInputStream进行的InputStream =新ByteArrayInputStream进行(cursor.getBlob(参数:columnIndex));
位图mBitmap = BitmapFactory.de codeStream(InputStream的);

When I'm retrieving image from the sqlite database my Bitmap object bm return null value can any one help me..?

I found problem in my database..

When I store the byte array in blob data type in database table that time the size of the byte array was 2280..

But when i retrieved that blob data type using select query I get the byte array within size 12.

My code is:

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

解决方案

Here's how I encode the image before writing to the database:

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

And then decode it like the from Cursor:

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

这篇关于安卓:问题检索数据库,从位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆