title: How to set a Image to a Image View from a URL Android
body: <p>I am trying to download and show  a image in my imageview from URL that i will get dyncamically .I have tried this way </p>
  
  <pre><code> URL url = new URL(parsedWeatherResponse.getWeatherIconUrl());
              Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
              weather_image.setImageBitmap(bmp);
  </code></pre>
  
  <p>But it showing exception that is </p>
  
  <pre><code>10-04 12:22:40.221: E/AndroidRuntime(15097): FATAL EXCEPTION: main
  10-04 12:22:40.221: E/AndroidRuntime(15097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.itintestapp/com.example.itintestapp.MainActivity}: android.os.NetworkOnMainThreadException
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.os.Handler.dispatchMessage(Handler.java:99)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.os.Looper.loop(Looper.java:153)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.ActivityThread.main(ActivityThread.java:4987)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at java.lang.reflect.Method.invoke(Method.java:511)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at dalvik.system.NativeStart.main(Native Method)
  10-04 12:22:40.221: E/AndroidRuntime(15097): Caused by: android.os.NetworkOnMainThreadException
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:70)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:185)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at com.example.itintestapp.MainActivity.setSectorData(MainActivity.java:64)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at com.example.itintestapp.MainActivity.onCreate(MainActivity.java:40)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.Activity.performCreate(Activity.java:5020)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
  10-04 12:22:40.221: E/AndroidRuntime(15097):    ... 11 more
  </code></pre>
  
  <p>Please help me how couuld i do this .I have show a image  from a URL</p>
  
  <p><strong>I have tried this way</strong> </p>
  
  <pre><code>class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
        ImageView bmImage;
  
        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }
  
        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
              InputStream in = new java.net.URL(urldisplay).openStream();
              mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }
  
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
      }
  </code></pre>
  
  <p><strong>Calling</strong></p>
  
  <pre><code> new DownloadImageTask((ImageView) findViewById(R.id.weather_image))
              .execute(parsedWeatherResponse.getWeatherIconUrl());
  </code></pre>
  
  <p>Getting this <strong>exception</strong> </p>
  
  <pre><code>10-04 12:29:14.298: E/AndroidRuntime(15813): FATAL EXCEPTION: main
  10-04 12:29:14.298: E/AndroidRuntime(15813): java.lang.NullPointerException
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at com.example.itintestapp.DownloadImageTask.onPostExecute(DownloadImageTask.java:32)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at com.example.itintestapp.DownloadImageTask.onPostExecute(DownloadImageTask.java:1)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at android.os.AsyncTask.finish(AsyncTask.java:631)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at android.os.Handler.dispatchMessage(Handler.java:99)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at android.os.Looper.loop(Looper.java:153)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at android.app.ActivityThread.main(ActivityThread.java:4987)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at java.lang.reflect.Method.invoke(Method.java:511)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
  10-04 12:29:14.298: E/AndroidRuntime(15813):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>