title: Android Image from Web
body: <p>I am working on a simple currency converter app and I want to grab an image from the web via a URL. I have worked with a number of tutorials, and none of them seem to work, even with simple copy and paste with no modification. Is there a problem in my XML, or just a stupid mistake in my java code?</p>
  
  <p><strong>XML</strong> The imageView is at the very bottom, and I'm just holding it with the default icon image, but it doesn't show up when I run the app.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#FFFFFF"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/Title"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:layout_marginTop="26dp"
          android:gravity="center"
          android:text="Currency Converter"
          android:textColor="#06BD27"
          android:textSize="24dp" /&gt;
  
      &lt;EditText
          android:id="@+id/editText1"
          android:layout_width="250dp"
          android:layout_height="wrap_content"
          android:layout_below="@+id/Title"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="53dp"
          android:text="Enter an Amount:" /&gt;
  
      &lt;Spinner
          android:id="@+id/spinner1"
          android:layout_width="250dp"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/editText1"
          android:layout_below="@+id/editText1"
          android:layout_marginTop="82dp"
          android:entries="@array/To"
          android:prompt="@string/ToCurrent" /&gt;
  
      &lt;Spinner
          android:id="@+id/spinner2"
          android:layout_width="250dp"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/spinner1"
          android:layout_below="@+id/editText1"
          android:layout_marginTop="23dp"
          android:entries="@array/From"
          android:prompt="@string/FromCurrent" /&gt;
  
      &lt;EditText
          android:id="@+id/editText2"
          android:layout_width="250dp"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/spinner1"
          android:layout_alignParentBottom="true"
          android:layout_marginBottom="18dp" /&gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/spinner1"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="66dp"
          android:src="@drawable/ic_launcher" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>Java Code</strong> createDrawableFromURL() is supposed to handle the image</p>
  
  <pre><code>package com.kimberly.currency_convertor;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  
  
  import android.app.Activity;
  import android.graphics.Typeface;
  import android.graphics.drawable.Drawable;
  import android.os.Bundle;
  import android.widget.ImageView;
  import android.widget.Spinner;
  import android.widget.TextView;
  
  public class CurrentConvert extends Activity
  {
      Spinner FirstSpin, SecondSpin;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.converter);
  
          FirstSpin = (Spinner)findViewById(R.id.spinner1);
          SecondSpin = (Spinner)findViewById(R.id.spinner2);
  
          TextView MainTitle = (TextView) findViewById(R.id.Title);  
          Typeface Currency = Typeface.createFromAsset(getAssets(), "CURRENCY.TTF"); 
          MainTitle.setTypeface(Currency); 
  
          ImageView imageView = (ImageView)findViewById(R.id.imageView1);
          imageView.setImageDrawable(createDrawableFromURL("http://savagelook.com/misc/sl_drop2.png"));
      }
  
      private Drawable createDrawableFromURL(String urlString) 
      {
          Drawable image = null;
      try 
      {
          URL url = new URL(urlString);
          InputStream is = (InputStream)url.getContent();
          image = Drawable.createFromStream(is, "src");
      } 
      catch (MalformedURLException e) 
      {
          // handle URL exception
          image = null;
      } catch (IOException e) 
      {
          // handle InputStream exception
          image = null;
      }
  
      return image;
      }
  }
  </code></pre>
  
  <p>Here is the code I have been trying to follow <a href="http://savagelook.com/blog/android/display-images-from-the-internet-in-android" rel="nofollow">http://savagelook.com/blog/android/display-images-from-the-internet-in-android</a></p>