android - Convert base64 string to Image in Java -
i have image being sent me through json string. want convert string image in android app , display image.
the json string looks this:
"data:image\/png;base64,ivborw0kggoaaaansuheugaaavi..." note: truncated string ...
i've got function (i think) converts string image. doing right?
public bitmap converttoimage(string image){ try{ inputstream stream = new bytearrayinputstream(image.getbytes()); bitmap bitmap = bitmapfactory.decodestream(stream); return bitmap; } catch (exception e) { return null; } } then try display on android activity this
string image = jsonobject.getstring("barcode_img"); bitmap mybitmap = this.converttoimage(image); imageview cimg = (imageview)findviewbyid(r.id.imageview1); //now try setting dynamic image cimg.setimagebitmap(mybitmap); however, when this, nothing shows up. don't errors in logcat. doing wrong?
thanks
i'm worried need decode base64 string image bytes, in your
"data:image\/png;base64,ivborw0kggoaaaansuheugaaavi..." string, must data after data:image\/png;base64,, image bytes , decode them:
string imagedatabytes = completeimagedata.substring(completeimagedata.indexof(",")+1); inputstream stream = new bytearrayinputstream(base64.decode(imagedatabytes.getbytes(), base64.default)); this code understand how works, if receive json object should done correct way:
- converting json string json object.
- extract string under
datakey. - make sure starts
image/pngknow png image. - make sure contains
base64string, know data must decoded. - decode data after
base64string image.
Comments
Post a Comment