Sit*_*ten 20
你应该BLOB在你的数据库中使用:
查看本教程 ......
但我认为你应该在HashMap中下载和存储图像,这样可以简化它.
码:
Stroring
Map<String, byte[]> hh = new HashMap<String, byte[]>();
String hi = "http://i.stack.imgur.com/TLjuP.jpg";
byte[] logoImagedata = getLogoImage(hi);
hh.put("img",logoImagedata);
Run Code Online (Sandbox Code Playgroud)
检索
byte[] imageByteArray = hh.get("img");
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
Run Code Online (Sandbox Code Playgroud)
getLogoImage()
private byte[] getLogoImage(String url){
try {
URL imageUrl = new URL(url);
URLConnection ucon = imageUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(500);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
return baf.toByteArray();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e.toString());
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
希望它能帮到你.
将图像存储到SQLite没有什么特别之处.只需使用BLOB记录类型创建表并执行以下操作:
protected long saveBitmap(SQLiteDatabase database, Bitmap bmp)
{
int size = bmp.getRowBytes() * bmp.getHeight();
ByteBuffer b = ByteBuffer.allocate(size); bmp.copyPixelsToBuffer(b);
byte[] bytes = new byte[size];
b.get(bytes, 0, bytes.length);
ContentValues cv=new ContentValues();
cv.put(CHUNK, bytes);
this.id= database.insert(TABLE, null, cv);
}
Run Code Online (Sandbox Code Playgroud)
也许你想要通过块来保存图像块,因为有限制/建议的BLOB大小(不记得多少)