Android : Loading an image from the Web with Asynctask?

If the image is not that big you can just use an anonymous class for the async task. This would like this: ImageView mChart = (ImageView) findViewById(R.id. Imageview); String URL = "..anything ..."; mChart.

SetTag(URL); new DownloadImageTask. Execute(mChart) The Task class: public class DownloadImagesTask extends AsyncTask { ImageView imageView = null; @Override protected Bitmap doInBackground(ImageView... imageViews) { this. ImageView = imageViews0; return download_Image((String)imageView.getTag()); } @Override protected void onPostExecute(Bitmap result) { imageView.

SetImageBitmap(result); } private Bitmap download_Image(String url) { ... } ding the URL in the tag is a bit tricky but it looks nicer in the calling class if you have a lot of imageviews that you want to fill this way. It also helps if you are using the ImageView inside a ListView and you want to know if the ImageView was recycled during the download of the image I wrote if you Image is not that big because this will result in the task having a implicit pointer to the underlying activity causing the garbage collector to hold the whole activity in memory until the task is finished. If the user moves to another screen of your app while the bitmap is downloading the memory can't be freed and it may make your app and the whole system slower.

If the image is not that big you can just use an anonymous class for the async task. This would like this: ImageView mChart = (ImageView) findViewById(R.id. Imageview); String URL = "..anything ..."; mChart.

SetTag(URL); new DownloadImageTask. Execute(mChart); The Task class: public class DownloadImagesTask extends AsyncTask { ImageView imageView = null; @Override protected Bitmap doInBackground(ImageView... imageViews) { this. ImageView = imageViews0; return download_Image((String)imageView.getTag()); } @Override protected void onPostExecute(Bitmap result) { imageView.

SetImageBitmap(result); } private Bitmap download_Image(String url) { ... } ding the URL in the tag is a bit tricky but it looks nicer in the calling class if you have a lot of imageviews that you want to fill this way. It also helps if you are using the ImageView inside a ListView and you want to know if the ImageView was recycled during the download of the image. I wrote if you Image is not that big because this will result in the task having a implicit pointer to the underlying activity causing the garbage collector to hold the whole activity in memory until the task is finished.

If the user moves to another screen of your app while the bitmap is downloading the memory can't be freed and it may make your app and the whole system slower.

– Hubert Jun 22 '10 at 7:07 it only is a problem if it takes longer then I guess 20 seconds to download the images and your activity is heavy on system memory. – Janusz Jun 22 '10 at 10:25 Thank you Janusz, I did an EDIT of my question with some added stuff. What do you think?

I would like to use this AsyncTask on different ImageViews in fact. How to I pass the correct reference to the ImageView within onPostExecute? Many thanks for your help.

– Hubert Jun 22 '10 at 10:40 You can't do it this way: mChart1. SetImageBitmap(new DownloadImagesTask(). Execute(graph_URL_1)); because the async task can not return something.

Returning would mean to block the execution until the task is ready but you don't want to do that. – Janusz Jun 22 '10 at 10:47 Have a look at my answer know. The use of the tag will make the calling class a little bit cleaner looking – Janusz Jun 22 '10 at 10:55.

This will get you images of any size... if you don't want the progress dialog just comment the codes in onPreExecute(); for(int I = 0 ; I SetMessage("fetching image from the server"); dialog.show(); } protected Bitmap doInBackground(String... args) { bitmap = getBitmapImageFromServer(); return bitmap; } protected void onPostExecute(Bitmap m_bitmap) { dialog.dismiss(); if(m_bitmap! = null) //store the images in an array or do something else with all the images. } } public Bitmap getBitmapImageFromServer(){ // fetch image form the url using the URL and URLConnection class }.

Thank you Umesh for your proposition. Maybe indeed I should also add a ProgressDialog, just in case! But how would you pass the context (your FileExplorer.

This) to the AsyncTask then? I would like that my DownloadImagesTask() could be accessed from any activity of my application ... I created a separate class for it. – Hubert Jun 22 '10 at 12:07.

You can create a class say..BkgProcess which contains an inner class that extends AsyncTask. While instantiating BkgProcess pass the context of your Activity class in BkgProcess constructor. For eg: public class BkgProcess { String path; Context _context; public Download(Downloader downloader, String path2){ this.

Path = path2; _context = downloader; } public void callProgressDialog(){ new BkgProcess(). Execute((Void)null); } class Downloads extends AsyncTask { private ProgressDialog dialog = new ProgressDialog(_context); protected void onPreExecute(){ dialog. SetMessage("Downloading image.."); dialog.show(); } protected void onPostExecute(Boolean success) { dialog.dismiss(); if(success) Toast.

MakeText(_context, "Download complete", Toast. LENGTH_SHORT).show(); } @Override protected Boolean doInBackground(Void... params) { return(startDownload(path)); } public boolean startDownload(String img_url) { // download img.. return true; } } } from your activity class.. BkgProcess dwn = new BkgProcess (Your_Activity_class. This, img_path); dwn.

CallProgressDialog().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions