在这里,我附加了将图像存储在 Drawable 中并以 BLOB 数据类型传递到数据库的代码。
我尝试了这个,它可以正确存储但不显示图像。
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);
还有其他解决方案来完成这件事吗?
为了从 sqlite 数据库获取图像,我使用了以下命令:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] data=cur.getBlob(cur.getColumnIndex("img"));
baos.write(data);
然后为了显示图像,我使用了以下操作:
InputStream is = database.getImageStream(someId);
Bitmap img = BitmapFactory.decodeStream(is);
下面的代码展示了如何将图像保存到 BLOB 字段中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(CompressFormat.PNG, 100, bos);
byte[] bytes = bos.toByteArray();
可以使用以下命令将字节添加到 sqlite
initialValues.put("<fieldname>", bytes); //ofcourse <fieldname> is of type BLOB
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)