从Android中的httpResponse获取图像内容

时间:2014-02-20 21:07:58

标签: android bitmap inputstream httpresponse httpentity

我试图从http响应中获取图像,但无法将流转换为位图。 请让我知道,我在这里失踪了什么。

仅供参考 - 图像内容以原始二进制文件形式接收。它是一个jpeg图像。

遵循程序:

  1. 制作HttpRequest。
  2. 作出回应检查200 - >获取httpentity内容。
  3. 使用BitMap工厂将流转换为位图。
  4. 将位图设置为imageview
  5. 在AsyncTask的postExecute中执行此操作

        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();
                }
        }
    }
    

    在AsyncTask的postExecute中执行此操作

        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);
        }
    

    先谢谢。

2 个答案:

答案 0 :(得分:5)

终于找到了答案。下面是片段 - 可能有助于使用http响应的新手。

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();
    String path = "/storage/emulated/0/YOURAPPFOLDER/FILENAME.EXTENSION";
    FileOutputStream output = new FileOutputStream(path);
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];
    int len = 0;
    while ((len = instream.read(buffer)) != -1) {
        output.write(buffer, 0, len);
    }
    output.close();
}

我们不是将文件保存到磁盘,而是将内容放在bytearray中并从中获取位图。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
try {
    // instream is content got from httpentity.getContent()
    while ((len = instream.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
    }
    baos.close();
} catch (IOException e) {
    e.printStackTrace();
}
byte[] b = baos.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView imageView = (ImageView)findViewById(R.id.picture_frame);
imageView.setImageBitmap(bmp);

仅供参考 - 在android fileoutput流中,写入本地磁盘必须在非UI线程中完成(在我的情况下使用了异步任务,并且此处未添加该部分)。

谢谢..

答案 1 :(得分:1)

使用此库处理来自网络的图像。 https://github.com/nostra13/Android-Universal-Image-Loader 它为你做了一切。 但是来自onPostExecute的代码应该放在onDoInBackground上。 onPre和onPost执行代码将在主线程上执行,doInBackground是工作线程。 但在这种情况下只使用通用图像加载器