The best way to upload an image and attach it to ImageView is to pass ImageView as a parameter in your async task and set the URL as the image tag, and then after loading the task in OnPostExecute() set the image to ImageView, look at this example:
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { ImageView imageView = null; @Override protected Bitmap doInBackground(ImageView... imageViews) { this.imageView = imageViews[0]; return download_Image((String)imageView.getTag()); } @Override protected void onPostExecute(Bitmap result) { imageView.setImageBitmap(result); } private Bitmap download_Image(String url) { ... }
And use will be that way
ImageView mChart = (ImageView) findViewById(R.id.imageview); String URL = "http://www...someImageUrl ..."; mChart.setTag(URL); new DownloadImageTask.execute(mChart);
The image will be automatically added after completion, for better memory optimization you can use WeakReference .
Good luck.
source share