我正在尝试捕获从 webview.capturePicture() 获取的图片并将其保存到 sqliteDatabase,我是否需要将图像转换为 byte[] 以便能够将其保存为表中的 BLOB ,然后能够检索该 byte[] 并将其转换回位图。
这是我正在做的事情:
Picture p = webView.capturePicture();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
p.writeToStream(bos);
byte[] ba = bos.toByteArray());
然后我通过以下方式检索图像:
byte[] image = cursor.getBlob(imageColumnIndex);
Bitmap bm = BitmapFactory.decodeByteArray(image, 0, image.length);
我能够很好地检索 byte[],但我始终从 bitmapfactory 获得空位图。
我还注意到,如果我 log.d(TAG, ""+bos) 我会按预期得到一长串字节,但如果我在执行 bos.toByteArray() 之后对 ba 执行相同操作,我只会得到一个短数组,像这样的事情:[B@2b0a7c60
我猜我在通过 OutputStream 转换为 byteArray 时可能遇到麻烦。这是因为 capturePiture() 方法返回 OutputStream 而不是 ByteArrayOutputStream 吗?
任何帮助,将不胜感激。
使用以下两个函数convert::::
public String convertBitmapToString(Bitmap src) {
String str =null;
if(src!= null){
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os);
byte[] byteArray = os.toByteArray();
str = Base64.encodeToString(byteArray,Base64.DEFAULT);
}
return str;
}
public static Bitmap getBitMapFromString(String src) {
Bitmap bitmap = null;
if(src!= null){
byte[] decodedString = Base64.decode(src.getBytes(), Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
}
return bitmap;
}
更新::
//Convert Picture to Bitmap
private static Bitmap pictureDrawable2Bitmap(Picture picture){
PictureDrawable pictureDrawable = new PictureDrawable(picture);
Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)