问题
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