問題描述
從 JSON 網絡服務檢索 android 中的 byte[] (Retrieve byte[] in android from JSON webservice)
I successfully stored image in database in BLOB format.
Data type for image in webservices model class is byte [].
Web service:
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("login")
public User Login(User input) {
Request:
[
{
"id": 2,
"password": "******",
"username": "haris",
"image": "iVBORw0KGgoAAAANSUhEUgAizDMizDMizDMizDMizDMizDMizDMizDMizDMizDMiz/JPL/AOc68RGNGkMYAAAAAElFTkSuQmCC"
}
]
We can see that the json service makes conversion to String, byte [] is converted into a String.
I tried to make the conversion from the String to byte [], but for result I can't see any picture data.
byte [] byteData = new byte[imageData().length()];
imageView.setImageBitmap(getImage(byteData));
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
‑‑‑‑‑
參考解法
方法 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));
}
(by Haris Dautović、Kapil Vats)