Retrieve byte[] in android from JSON webservice

只谈情不闲聊 提交于 2019-12-23 05:39:06

问题


I successful store image in database BLOB format.

DataType for image in webservices model class is byte [].

My web services:

@POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("login")
    public Korisnici Login(Korisnici k) {

        Korisnici exception = new Korisnici(); 

............

Json webservice example:

[
    {
        "id": 2,
        "ime": "haris",
        "mjestoGoogleMaps": null,
        "password": "1234",
        "username": "haris",
        "image": "iVBORw0KGgoAAAANSUhEUgAizDMizDMizDMizDMizDMizDMizDMizDMizDMizDMiz/JPL/AOc68RGNGkMYAAAAAElFTkSuQmCC"
    }
]

we see that the json service makes conversion to String, byte [] is converted into a string.

I tried to make the conversion String to byte [] in Android, but I do not see a picture.

Here is my code :

byte [] b = new byte[getString_Image().length()];

profilna.setImageBitmap(getImage(b));

convert from byte array to bitmap

public static Bitmap getImage(byte[] image) {
        return BitmapFactory.decodeByteArray(image, 0, image.length);
}

How to resolve this problem?


回答1:


use like this

byte[] decodedString = Base64.decode(img, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (bitmap != null) {
Drawable image = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 90, 100, true));
}


来源:https://stackoverflow.com/questions/15021526/retrieve-byte-in-android-from-json-webservice

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