title: How to wait for downloading thread to finish and then return without blocking UI thread?
body: <p>I am trying to make a class to download an image, store it and then return it as a <code>Drawable</code>. The file need not be downloaded multiple times, but only just the first time.</p>
  
  <p>Here is my class :</p>
  
  <pre><code>package com.ishan.drawabletest;
  ..........................
  public class DrawableProvider extends ContextWrapper{
  
  Drawable resultDrawable;
  
  public DrawableProvider(Context base) {
      super(base);
  }
  
  public Drawable get(final String fileURL) {
      // File name that is being downloaded
      String downloadedFileName = fileURL.substring(fileURL.lastIndexOf("/")+1);
      final String imagePath = getFilesDir() + "/" + downloadedFileName;
  
      // Checking if the file exists or not
      final File file = new File(imagePath);
      if(file.exists()) {
          // The image already exists, have to only return it by converting into drawable
          resultDrawable = Drawable.createFromPath(imagePath);
          return resultDrawable;
      } else {
          // The image doesn't exists already.
          Thread t = new Thread() {
              @Override
              public void run() {
                  try {
                      // Downloading the image here
                      ..................
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  resultDrawable = Drawable.createFromPath(imagePath);
              }
          };
          t.start();
          // Waiting for the thread to finish before returning using join
          try {
              t.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          return resultDrawable;
      }
  }
  }
  </code></pre>
  
  <p>and this class is called and used like this. :</p>
  
  <pre><code>ImageView permanentImageView = (ImageView) findViewById(R.id.permaImageView);
      DrawableProvider dp = new DrawableProvider(this);
      permanentImageView.setImageDrawable(dp.get("http://youthvibe2014server.herokuapp.com/public/selenaGomez.jpg"));
  </code></pre>
  
  <p>It works fine for the cases when the image is already available. The problem is that if I <strong>do not wait</strong> ( i.e. if join is removed ) for the thread to finish and return immediately, then the screen does not contains an Image not even the default image (defined in XML) which should be there till the time the download finishes.</p>
  
  <p>It looks like this.</p>
  
  <p><img src="https://i.stack.imgur.com/akNVr.png" alt="The screen without the image or even without the default image."></p>
  
  <p>I guess it is like this because my <code>get</code> method isn't waiting for the thread to finish and returning <code>null</code>, thus the <code>permanentImageView</code> is containing <code>null</code>.</p>
  
  <p>But if <strong>I do wait</strong> for the thread to finish. The screen eventually displays the downloaded image, but for the time being it goes blank, and looks like this. :</p>
  
  <p><img src="https://i.stack.imgur.com/gpDx2.png" alt="The blank screen when waiting for thread to finish"></p>
  
  <p>I guess this is because when I am waiting for the thread to finish my UI thread is blocked for the time being.</p>
  
  <p>I can't use <code>AsyncTask</code> since I am extending <code>ContextWrapper</code> and thus can't also extend <code>AsyncTask</code>. Is there a way to wait for my thread to finish and then return without blocking my UI thread ?</p>