title: BitmapFactory.decodeStream returns null, when downloading a image form web
body: <p>I'm trying to download a image from a URL, using the <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap" rel="nofollow noreferrer">Google Example Page</a>. I've <a href="https://stackoverflow.com/questions/10730520/decodestream-returns-null">read</a> 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.</p>
  
  <p>This is my code:</p>
  
  <p>This part is in a doInBackground method in a AsyncTask class</p>
  
  <pre><code>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 &gt; reqHeight || width &gt; reqWidth) {
           if (width &gt; 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);
   }
   }
  </code></pre>