【发布时间】:2015-01-28 15:08:48
【问题描述】:
我正在尝试从经过身份验证的站点下载图像。该站点返回一个 base64 版本的图像。这是改造的正确方法吗?如何获取图像并设置为我的图像视图。
@GET("/img/avatars/{id}")
public void getProfilePic(@Path("id") int id,
Callback<TypedByteArray> result);
我将我的恢复适配器日志设置为完整,响应值如下所示
���V�3��Ωw���Tw�5�vT��>8u�`�j�S�������#���%�A���"Xw��Oq������G@]éG���f�~A#lD�)<���•
不是 base64 字符串。
我尝试过的
customResAdapter(ImageService.class).getProfilePic(id, new Callback<TypedByteArray>() {
@Override
public void success(TypedByteArray result, Response response) {
try {
byte[] decodedString = Base64.decode(result.getBytes(), Base64.DEFAULT);
mProfilePic.setImageBitmap(BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failure(RetrofitError error) {
}
});
我不知道以下代码是否正确,但目前我收到此错误消息
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:应为 BEGIN_OBJECT 但为 STRING 在第 1 行第 1 列路径
【问题讨论】:
-
你需要将base64字符串解码为位图,
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); -
看起来您在将字节数组转换为 utf 后正在记录它