Search code examples
androidbitmapbitmapfactory

BitmapFactory.decodeStream returns null, when downloading a image form web


I'm trying to download a image from a URL, using the Google Example Page. I've read when I use a InputStream in the BitmapFactory.decodeStream method, I can't use twice. I'm trying to do that, but it doesn't work 'cause it returns null in the decoded image, and I don't know what I can do.

This is my code:

This part is in a doInBackground method in a AsyncTask class

Bitmap bitmapImage;
URL imageUrl = null;
try {
imageUrl = new URL(url[0]);

HttpGet httpRequest = null;
httpRequest = new HttpGet(imageUrl.toURI());

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();

    bitmapImage = CommonMethods.decodeSampledBitmapFromResource(instream, thumb_width, thumb_width);

instream.close();
return bitmapImage;

 } catch (URISyntaxException e) {
    e.printStackTrace();
    return null;
 } catch (MalformedURLException e) {
    e.printStackTrace();
    return null;
 } catch (IOException e) {
    e.printStackTrace();
    return null;
 }


 public static Bitmap decodeSampledBitmapFromResource(InputStream instream,
        int reqWidth, int reqHeight) throws IOException {

    //Copy instream for decode twice 
ByteArrayOutputStream out = new ByteArrayOutputStream();
    copy(instream,out);
    ByteArrayInputStream instream2 = new ByteArrayInputStream(out.toByteArray());

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(instream, null, options);
    instream2.close();

    options.inJustDecodeBounds = false;

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    return BitmapFactory.decodeStream(instream, null, options);
 }

 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

     // Raw height and width of image
     final int height = options.outHeight;
     final int width = options.outWidth;
     int inSampleSize = 1;

     if (height > reqHeight || width > reqWidth) {
         if (width > height) {
         inSampleSize = Math.round((float) height / (float) reqHeight);
     } else {
             inSampleSize = Math.round((float) width / (float) reqWidth);
     }
     }

     return inSampleSize;
}

//Copy instream method
public static void copy(InputStream input, OutputStream output) throws IOException{

     byte[] buffer = new byte[Constants.IO_BUFFER_SIZE];

 int n = 0;

 while (-1 != (n = input.read(buffer))) {

     output.write(buffer, 0, n);
 }
 }

Solution

  • Found the code which will work for you

    final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        inputStream = entity.getContent();
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        //options.inSampleSize = 2;
                        final Bitmap bitmap = BitmapFactory
                                .decodeStream(inputStream, null, options);
                        return bitmap;
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
    

    Please replace the proper variable and if you wanted to scale the image you can scale it after getting the bitmap.