Get image content from httpResponse in Android

Dinesh picture Dinesh · Feb 20, 2014 · Viewed 7.2k times · Source

Am trying to get a image from a http response, but am failing to convert the stream to bitmap. Please let me know, what am i missing here.

FYI - the image content is received as raw binary & its a jpeg image.

Procedure followed:

  1. Make HttpRequest.
  2. In response check for 200 -> get the httpentity content.
  3. convert the stream to bitmap using BitMap factory.
  4. Set the bitmap to imageview

Doing this in postExecute of the 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();
            }
    }
}

Doing this in postExecute of the 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);
    }

Thanks in Advance.

Answer

Dinesh picture Dinesh · Feb 24, 2014

Finally found the answer for this. below is the snippet - might help for the newbees working with http responses.

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

Instead of saving the file to disk, we can have the contents in the bytearray and get the bitmap from that.

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

FYI - In android fileoutput stream writing to local disk have to be done in non UI thread(have used async task in my case & that part is not added here).

Thanks ..