可绘制对象转换为BLOB数据类型的SQLite问题 [英] Convert Drawable to BLOB Datatype sqlite problem

查看:134
本文介绍了可绘制对象转换为BLOB数据类型的SQLite问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我附上code,其中存储图像中绘制对象,并传递到数据库BLOB数据类型。

Here I am attaching code in which store image in Drawable and pass to Database with BLOB Datatype.

我尝试这一点,将其存储正确的,但不能显示图像。

I try this, it store properly but not display image.

Insall_app_db i1 = new Insall_app_db();
i1.createDatabse(DB, getBaseContext());
i1.createTable(tableName, getBaseContext());

BitmapDrawable bitDw = ((BitmapDrawable) icon);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
String iconStr = imageInByte.toString();
i1.insertDataUser(tableName,appid,appname2,pname, versionName, versionCode,iconStr, long_date);
int appid = i1.GetUserData(getBaseContext(), tableName);

System.out.println("-------------------------------");
byte[] imaaa = iconStr.getBytes();

Bitmap aaa = convertByteArrayToBitmap(imaaa);

LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon,null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(i1.app_name);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
Bitmap bitmap2 = ((BitmapDrawable)icon).getBitmap();
iv.setImageBitmap(aaa);

有没有其他解决办法做这件事情?

Is there any other solution to do this thing?

推荐答案

要得到SQLite数据库图像我用下面的:

To get the image from the sqlite database i used the following:

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 byte[] data=cur.getBlob(cur.getColumnIndex("img"));
 baos.write(data);

随后,以显示我用来做如下的图像:

Then to display the image i used to do the following:

 InputStream is = database.getImageStream(someId);
 Bitmap img = BitmapFactory.decodeStream(is);

在code以下显示了如何将图像保存到一个BLOB字段

The code below shows how to save the image into a BLOB field

ByteArrayOutputStream bos = new ByteArrayOutputStream();      
image.compress(CompressFormat.PNG, 100, bos); 
byte[] bytes = bos.toByteArray(); 

字节可以添加使用SQLite的

the bytes can be added to sqlite using

 initialValues.put("<fieldname>", bytes); //ofcourse <fieldname> is of type BLOB

这篇关于可绘制对象转换为BLOB数据类型的SQLite问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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