title: Android: Bug with ThreadSafeClientConnManager downloading images
body: <p>For my current application I collect images from different "event
  providers" in Spain.</p>
  
  <pre><code>  Bitmap bmp=null;
    HttpGet httpRequest = new HttpGet(strURL);
  
    long t = System.currentTimeMillis();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    Log.i(TAG, "Image ["+ strURL + "] fetched in [" + (System.currentTimeMillis()-t) + "ms]");
  
       HttpEntity entity = response.getEntity();
       InputStream instream = entity.getContent();
       bmp = BitmapFactory.decodeStream(instream);
  
       return bmp;
  </code></pre>
  
  <p>However, when downloading images from salir.com I get the following
  logcat output:</p>
  
  <pre><code>13970     Gallery_Activity  I  Fetching image 2/8 URL: http://media.salir.com/_images_/verticales/a/0/1/0/2540-los_inmortales_la_trattoria-marc_aureli_27_29_no.jpg
  13970     ServiceHttpRequest  I  Image [http://media.salir.com/_images_/verticales/a/0/1/0/2540-los_inmortales_la_trattoria-marc_aureli_27_29_no.jpg] fetched in [146ms]
  13970     skia  D  --- decoder-&gt;decode returned false
  </code></pre>
  
  <p>A search for that error message didn't provide much useful results.</p>
  
  <p>Anyone an idea what the problem could be?</p>
  
  <p>Gracias! </p>
  
  <hr>
  
  <p><strong>Update 1:</strong></p>
  
  <p>After inquiring a bit more and testing different stuff I figured out that the problem seems to lie somewhere else. Even though my logcat output says</p>
  
  <pre><code>13970     ServiceHttpRequest  I  Image [http://media.salir.com/_images_/verticales/a/0/1/0/2540-los_inmortales_la_trattoria-marc_aureli_27_29_no.jpg] fetched in [146ms]
  getContentLength(): 93288
  </code></pre>
  
  <p>which is the correct length of the image in bytes it seems that there's something wrong with the stream or HTTP connection.</p>
  
  <p>My original code (above) is taking advantage of the <a href="http://developer.android.com/reference/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html" rel="nofollow noreferrer">ThreadSafeClientConnManager</a>. If I replace it with just a simple <code>URLConnection</code> it works perfectly:</p>
  
  <pre><code>URL url = new URL(strURL);
  URLConnection conn = url.openConnection();
  conn.connect();
  InputStream instream = conn.getInputStream();
  bmp = BitmapFactory.decodeStream(instream);
  </code></pre>
  
  <p>So, I'm wondering now why does my <code>ThreadSafeClientConnManager</code> work flawlessly (at least it seems it does) with all my other connections (mostly exchanging <code>JSONObjects</code>) but not with images from some specific websites (e.g. salir.com - for most other websites it works, though). Is there a HTTP parameter I'm missing?</p>
  
  <p>My current setup is:</p>
  
  <pre><code>HttpParams parameters = new BasicHttpParams();
  HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
  HttpProtocolParams.setContentCharset(parameters, HTTP.UTF_8);
  HttpProtocolParams.setUseExpectContinue(parameters, false); // some webservers have problems if this is set to true
  ConnManagerParams.setMaxTotalConnections(parameters, MAX_TOTAL_CONNECTIONS);
  HttpConnectionParams.setConnectionTimeout(parameters, CONNECTION_TIMEOUT);
  HttpConnectionParams.setSoTimeout(parameters, SOCKET_TIMEOUT);
  
  SchemeRegistry schReg = new SchemeRegistry();
  schReg.register(new Scheme("http", 
       PlainSocketFactory.getSocketFactory(), HTTP_PORT));
  
  ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg);
  
  DefaultHttpClient http_client = new DefaultHttpClient(conMgr, parameters);
  </code></pre>
  
  <hr>
  
  <p><strong>Update 2:</strong></p>
  
  <p>Now, the strange thing is, that it actually does work with the <code>ThreadSafeClientConnManager</code> <strong>-sometimes-</strong>. If I keep trying downloading the image and decoding it for a couple of times in a row it might work after 15-30 trials. Very strange.</p>
  
  <p>I hope there's a solution to that since I would prefer using the <code>ThreadSafeClientConnManager</code> instead of <code>URLConnection</code>.</p>
  
  <hr>
  
  <p><strong>Update 3:</strong></p>
  
  <p>As suggest by Mike Mosher below, it seems that by using <a href="http://developer.android.com/intl/fr/reference/org/apache/http/entity/BufferedHttpEntity.html" rel="nofollow noreferrer">BufferedHttpEntity</a> the <em>decoding error</em> doesn't appear any more. However now, even though less often than before, I get a <code>SkImageDecoder::Factory returned null</code> error.</p>