Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot load image from a Url using BitmapFactory.decodeStream()
    primarykey
    data
    text
    <p>I am trying to download an image from a URL to display as an ImageView. The download is done in the background using AsyncTask. However, the call to the decodeStream of the BitmapFactory always returns a null object. I verified that the Url provided for the connection is right, but it seems that BitmapFactory cannot read the image from the InputStream returned by the HTTP connection. Here is the code below:</p> <pre><code>@Override protected Bitmap doInBackground(String... uri) { Bitmap bm = null; HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(Uri.encode(uri[0])); try { HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); String contentType = entity.getContentType().getValue(); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int halfScreen = metrics.widthPixels / 2; int photoWidth = halfScreen &gt; 200 ? 200 : halfScreen; if (contentType.contains("image/jpeg") || contentType.contains("image/png") || contentType.contains("image/gif")) { bm = BitmapFactory.decodeStream(new BufferedInputStream(entity.getContent())); if (bm.getWidth() &gt; photoWidth) bm = Bitmap.createScaledBitmap(bm, photoWidth, Math.round((photoWidth*bm.getHeight())/bm.getWidth()), true); } } catch (Exception e) { bm = null; } return bm; } </code></pre> <p>What is weird is that the exact same code runs fine on a Nexus S, but does not work on a Samsung running Android 2.1-update1.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload