Updating an ImageView with an image from a URL
<p>Within my android application, I'm wanting to update an ImageView, with an image specified from a URL. This URL has been fetched via an API as part of an AsyncTask.</p>

<p>I have defined the ImageView as follows:</p>

<pre><code>&lt;ImageView
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_height="wrap_content" 
    android:id="@+id/product_image"
    android:src="@drawable/no_product_image" 
    android:layout_marginRight="6dip"
    android:layout_width="wrap_content"
    /&gt;
</code></pre>

<p>That way, whilst the data is loading, there is a blank image present behind a ProgressDialog.</p>

<p>As part of the doInBackground of the AsyncTask, after fetching the product details, I call:</p>

<pre><code>private void createProductScreen(Product product){
        Log.i(TAG, "Updating product screen");

        //Set the product image
        ImageView imgView =(ImageView)findViewById(R.id.product_image);
        Drawable drawable = loadImageFromWeb(product.getImageUrl());
        imgView.setImageDrawable(drawable);
        Log.i(TAG, "Set image");
}
</code></pre>

<p>The image is loading from the web as follows:</p>

<pre><code>private Drawable loadImageFromWeb(String url){
        Log.i(TAG, "Fetching image");
        try{
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src");
            Log.i(TAG, "Created image from stream");
            return d;
        }catch (Exception e) {
            //TODO handle error
            Log.i(TAG, "Error fetching image");
            System.out.println("Exc="+e);
            return null;
        }
    }
</code></pre>

<p>When this runs I see <em>"Created image from stream"</em> in the log, but don't see <em>"Set image"</em> and the onPostExecute of the AsyncTask is never called.</p>

<p>Does anyone have any ideas on the issue or a better way of updating the image from the blank image to the product image?</p>
