user1282637
user1282637

Reputation: 1867

How to return a Bitmap from an Async Task in Android app

Ok, so this code is right off the Android Developer site which sets an ImageView to a Bitmap:

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;

public BitmapWorkerTask(ImageView imageView) {
    // Use a WeakReference to ensure the ImageView can be garbage collected
    imageViewReference = new WeakReference<ImageView>(imageView);
}

// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
    data = params[0];
    return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
}

// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
    if (imageViewReference != null && bitmap != null) {
        final ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}
}

And basically, what I want to do is instead of doing

imageView.setImageBitmap(bitmap); I want to return that Bitmap so I can then use it in my Main UI. Is there any way of going about doing this? Sorry about my lack of knowledge on AsyncTask, I just started learning it recently. Thank you!

Upvotes: 2

Views: 11954

Answers (4)

umerk44
umerk44

Reputation: 2817

Actually there is no need to return bitmap and than use it in main Ui Thread. You can do this on PostExecute becuase postExecute runs in UI Thread. Your question?? Yes you can return bitmap from AsynTask you can do something like this

private class myTask extends AsyncTask<Void,Void,Bitmap>{


      protected Bitmap doInBackground(Void... params) {

            //do stuff
             return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            //do stuff

            }
        }

Now you can get bitmap by calling

 Bitmap returned_bitmap = new myTask().execute().get()

Again this is not good this will hang your UI. But you can get value from Aysnc like this.

Another method you can do is by implementing callback using interface

public interface MyInterface 
{
   public void onPostExecute();
}

Activity class

 public class MyActivity extends Activity implements MyInterface{

private Bitmap Image;

public void onCreate(Bundle b)
{
    super.onCreate(b);

    new MyTask(this).execute();
}

@Override
public void onPostExecute() {
        //Now you have your bitmap update by do in background 
    //do stuff here 
}


private class MyTask extends AsyncTask<Void, Void, Void>
{
    MyInterface myinterface;

    MyTask(MyInterface mi)
    {
        myinterface = mi;
    }

    @Override
    protected Void doInBackground(Void... params) {
        //firt declare bitmap as class member of MyActivity 
        //update bitmap here and then call

        myinterface.onPostExecute();

        return null;
    }

}

 }

Upvotes: 12

ashishduh
ashishduh

Reputation: 6699

First of all, onPostExecute executes on your UI thread, so you don't need to do anything differently from the example. But you can always use anonymous classes if you want:

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        AsyncTask task = new BitmapWorkerTask() {
            @Override
            public void onPostExecute(Bitmap bitmap) {
                // Do whatever
            }
        };

        task.execute();
    }
}

Upvotes: 0

Mika
Mika

Reputation: 5845

You should save yourself a lot of time and use Picasso. This will save you all of this code and it works with bitmaps. Basically you will write something like this:

Picasso.with(context).load("www.somthing.com/api/blah").placeholder(R.drawable.default_picture).into(imageView);

It's one line of code and you got Async and caching for free...

Upvotes: 1

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You could either use a callback method through interface or put the asynctask as a inner class and just call the some method for setting the imageView...

For the callback there is a good example here.

Upvotes: 0

Related Questions