title: Android : Loading an image from the Web with Asynctask
body: <p>How do I replace the following lines of code with an Asynctask ? 
  How do you "get back" the Bitmap from the Asynctask ? Thank you.</p>
  
  <pre><code>ImageView mChart = (ImageView) findViewById(R.id.Chart);
  String URL = "http://www...anything ...";
  
  mChart.setImageBitmap(download_Image(URL));
  
  public static Bitmap download_Image(String url) {
  
          //---------------------------------------------------
          Bitmap bm = null;
          try {
              URL aURL = new URL(url);
              URLConnection conn = aURL.openConnection();
              conn.connect();
              InputStream is = conn.getInputStream();
              BufferedInputStream bis = new BufferedInputStream(is);
              bm = BitmapFactory.decodeStream(bis);
              bis.close();
              is.close();
          } catch (IOException e) {
              Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
          } 
          return bm;
          //---------------------------------------------------
  
      }
  </code></pre>
  
  <p>I thought about something like this :</p>
  
  <p>replace :</p>
  
  <pre><code>mChart.setImageBitmap(download_Image(graph_URL));
  </code></pre>
  
  <p>by something like :</p>
  
  <pre><code>mChart.setImageBitmap(new DownloadImagesTask().execute(graph_URL));
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class DownloadImagesTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
  
  @Override
  protected Bitmap doInBackground(String... urls) {
      return download_Image(urls[0]);
  }
  
  @Override
  protected void onPostExecute(Bitmap result) {
      mChart.setImageBitmap(result);              // how do I pass a reference to mChart here ?
  }
  
  
  private Bitmap download_Image(String url) {
      //---------------------------------------------------
      Bitmap bm = null;
      try {
          URL aURL = new URL(url);
          URLConnection conn = aURL.openConnection();
          conn.connect();
          InputStream is = conn.getInputStream();
          BufferedInputStream bis = new BufferedInputStream(is);
          bm = BitmapFactory.decodeStream(bis);
          bis.close();
          is.close();
      } catch (IOException e) {
          Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
      } 
      return bm;
      //---------------------------------------------------
  }
  
  
  }
  </code></pre>
  
  <p>but How do I pass a reference to mChart in onPostExecute(Bitmap result) ???
  Do I need to pass it with the URL in some way ?
  I would like to replace all my lines of code :</p>
  
  <pre><code>mChart1.setImageBitmap(download_Image(URL_1));
  mChart2.setImageBitmap(download_Image(URL_2));
  </code></pre>
  
  <p>with something similar ... but in Asynctask way !</p>
  
  <pre><code>mChart1.setImageBitmap(new DownloadImagesTask().execute(graph_URL_1));
  mChart2.setImageBitmap(new DownloadImagesTask().execute(graph_URL_2));
  </code></pre>
  
  <p>Is there an easy solution for this ?
  Do I get something wrong here ?</p>