title: How to show an image from an url in android
body: <p>I have looked around the internet but haven't been able to find a <strong>working</strong> example on how to show an image from an url.</p>
  
  <p>The below code is crashing and I can't find out why.. Any help is greatly appreciated. Thanks.</p>
  
  <p>TesteImages.java:</p>
  
  <pre><code>package pac.image3;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.Bundle;
  import android.widget.ImageView;
  
  public class TestImages extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  //        setContentView(R.layout.main);
          ImageView image = (ImageView) findViewById(R.id.test_image);
  
          String imageUrl = "http://www.small-world.net/_borders/small_world_logo.gif";
          try {
            ImageView i = (ImageView)findViewById(R.id.test_image);
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
            i.setImageBitmap(bitmap);
            setContentView(image);
          } catch (MalformedURLException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
  
  
      }
  }
  </code></pre>
  
  <p>res/layout/main.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
  &lt;TextView  
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      /&gt;
  
      &lt;ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>