Android - How to convert picture from webview.capturePicture() to byte[] and back to bitmap

廉价感情. 提交于 2019-12-03 16:47:01

Use the below two function 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;
}

Updated::

//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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!