我必须在 ImageView 中以 json 格式显示从服务器获取的图像。但是我得到的不是图片网址,而是很多这样的字符..
����JFIF��C
$.' ",#(7),01444'9=82<.342��C
2!!22222222222222222222222222222222222222222222222222����"��
���}!1AQa"q2���#B��R��$3br�
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�����
我猜这是图像的原始数据或 JFIF。我搜索了 stackoverflow,但只能找到从 base64 转换,或显示来自 url 的图像。
如何从中获取图像并将其显示在 ImageView 中。
问候。
最佳答案
我不必将响应转换为 Base64。只是将其转换为位图并将其设置为 ImageView。由于我缺乏知识,这是一个非常愚蠢的问题。希望我的代码对以后的人有帮助
HttpClient httpClient = ServiceHelper.getHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(RestApiPaths.GET_PROFILE_PICTURE);
try {
HttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(emergencyCardPhoto);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
关于Android,json 中的 JFIF responseText - ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30302658/