title: Returning a bitmap from AsyncTask
body: <p>Following this tutorial:
  <a href="http://developer.android.com/training/displaying-bitmaps/process-bitmap.html" rel="nofollow">http://developer.android.com/training/displaying-bitmaps/process-bitmap.html</a></p>
  
  <p>It says to use AsyncTask to load bitmaps. Sounds great, but in their example it uses an ImageView. I don't want to use an ImageView, I've created my own View that I will draw to. After I load the bitmap in the AsyncTask, how can I return the newly loaded bitmap to the main thread?</p>
  
  <pre><code>    private Bitmap mLoadedBitmap;
  
          public void loadBitmap(String path, int width, int height) {
              BitmapWorkerTask task = new BitmapWorkerTask(mLoadedBitmap);
              BitmapTaskParams params = new BitmapTaskParams(path, width, height);
  
              task.execute(params);
  
          }
  
          public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
              final int height = options.outHeight;
              final int width = options.outWidth;
              int inSampleSize = 1;
  
              if(height &gt; reqHeight || width &gt; reqWidth) {
                  final int halfHeight = height / 2;
                  final int halfWidth = width / 2;
                  // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                  // height and width larger than the requested height and width.
                  while ((halfHeight / inSampleSize) &gt; reqHeight
                          &amp;&amp; (halfWidth / inSampleSize) &gt; reqWidth) {
                      inSampleSize *= 2;
                  }
              }
  
              return inSampleSize;
          }
  
          public static Bitmap decodeSampledBitmapFromFile(String file, int reqWidth, int reqHeight) {
              final BitmapFactory.Options options = new BitmapFactory.Options();
              options.inJustDecodeBounds = true;
              BitmapFactory.decodeFile(file, options);
  
              options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
              options.inJustDecodeBounds = false;
              return BitmapFactory.decodeFile(file, options);
          }
  
  
          private static class BitmapTaskParams {
              private String path;
              private int width; 
              private int height;
  
              BitmapTaskParams(String path, int width, int height) {
                  this.path = path;
                  this.width = width;
                  this.height = height;
              }
          }
  
  
          class BitmapWorkerTask extends AsyncTask&lt;BitmapTaskParams, Void, Bitmap&gt; {
              private final WeakReference&lt;Bitmap&gt; bitmapReference;
  
              private String path;
              private int width;
              private int height;
  
              public BitmapWorkerTask(Bitmap bmp) {
                 bitmapReference = new WeakReference&lt;Bitmap&gt;(bmp);
              }
  
              // Decode image in background.
              @Override
              protected Bitmap doInBackground(BitmapTaskParams... params) {
                  path = (String)params[0].path;
                  width = (Integer)params[0].width;
                  height = (Integer)params[0].height;
  
                  return decodeSampledBitmapFromFile(path, width, height);
              }
  
              // Once complete, see if ImageView is still around and set bitmap.
              @Override
              protected void onPostExecute(Bitmap bitmap) {
                  if(bitmap != null) {
                      Bitmap bmp = bitmapReference.get();
                      if(bmp != null) {
                          bmp = bitmap;
  //I copied this from the example. How can I actually return the bitmap to a bitmap in my main class / main thread?
  
                      }
                  }
              }
          }
  </code></pre>