我试图从http响应中获取图像,但未能将该流转换为位图。 请让我知道,我在这里错过了什么。从Android的httpResponse获取图像内容
仅供参考 - 图像内容以原始二进制形式接收&其jpeg图像。
步骤如下:
- 制作的HttpRequest。
- 作为响应,请检查200 - >获取httpentity内容。
- 使用BitMap工厂将流转换为位图。
- 将位图的ImageView
中的的AsyncTask
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(endpoint);
// Adding Headers ..
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
return instream;
// instream.close();
}
}
}
postExecute这样做在的AsyncTask
if (null != instream) {
Bitmap bm = BitmapFactory.decodeStream(instream);
if(null == bm){
Toast toast = Toast.makeText(getApplicationContext(),
"Bitmap is NULL", Toast.LENGTH_SHORT);
toast.show();
}
ImageView view = (ImageView) findViewById(R.id.picture_frame);
view.setImageBitmap(bm);
}
致谢postExecute提前这样做。
谢谢,不想使用其他库。目的是在单个图书馆中处理所有请求和响应。 – Dinesh