Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async task android onPostExecute

Tags:

android

In my android application i use AsyncTask to download the image from internet.

I pass the url and my AsyncTask does the download. But how do i return the bitmap back to my activity. Currently i pass the callback function to my asynctask constructor and in the OnPostExecute i invoke the callback function. Is this the correct way to do it.

like image 999
Vinoth Avatar asked Mar 22 '11 11:03

Vinoth


2 Answers

You can do something like this:

public Bitmap btm;
public void onClick(View v) {
  new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
     protected Bitmap doInBackground(String... urls) {
         return loadImageFromNetwork(urls[0]);
     }

     protected void onPostExecute(Bitmap result) {
         // set image for ImageView
         mImageView.setImageBitmap(result);
         // orsimply save it
         btm = result;
     }
 }
like image 150
Anton Derevyanko Avatar answered Sep 28 '22 16:09

Anton Derevyanko


The result you return from doInBackground is passed to onPostExecute so simple return the bitmap from doInBackground method and handle it in onPostExecute.

like image 37
Mojo Risin Avatar answered Sep 28 '22 14:09

Mojo Risin