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.
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;
}
}
The result you return from doInBackground
is passed to onPostExecute
so simple return the bitmap from doInBackground
method and handle it in onPostExecute
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With