SQLite可以存储 BLOB(binary large object,二进制大对象)格式数据,利用它可以在安卓应用开发中存储图片资源。
这里先讲下,怎样把数据从数据库中取出,并显示在imagView中。
代码如下:
private ImageView pic; pic = (ImageView) findViewById(R.id.pic_school); pic.setImageDrawable(dbm.blobToDrawable(cursor.getBlob(4))); public Drawable blobToDrawable(byte [] fromBlob){ Drawable forChange = null; if(fromBlob.length == 0) return forChange; ByteArrayInputStream msgContent = new ByteArrayInputStream(fromBlob); forChange = Drawable.createFromStream(msgContent, "image"); return forChange; }
另外长字符串的转换如下:
public String blobToString(byte [] fromBlob ){ String forChange = ""; if(fromBlob.length == 0) return forChange; forChange = new String(fromBlob); return forChange; }