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

11,365

Solution 1

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;
    }

}

 }

Solution 2

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...

Share:
11,365
user1282637
Author by

user1282637

Updated on June 05, 2022

Comments

  • user1282637
    user1282637 almost 2 years

    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!