0

I am trying to write an AsyncTask which takes an ImageView and path to the image as input and loads the image in a background thread. I wanted to know how I can pass both the ImageView and the path to the AsyncTask? The Params argument in the doInBackground can hold an array of data belonging to same class, but this is a different scenario as one of them is a View and another one is the path to a file (String).

One option I have, is to have the AsyncTask as an inner class and declare the ImageView as a class variable which can be accessed inside AsyncTask. I don't want to do this, because the same task is used by multiple classes and it doesn't conform to the DRY policy.

Any help is appreciated.

Thanks.

500865
  • 6,920
  • 7
  • 44
  • 87

3 Answers3

1

The same type of question asked in SO Android : Loading an image from the Web with Asynctask and also look at Trying to use AsyncTask do download some image files try it and if you found problem let me know it.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
1

you can use the ImageView's setTag method, and give it the path of your image. your asynctask or background thread can then getTag your imageView and do whatever you want with it.

josephus
  • 8,284
  • 1
  • 37
  • 57
1

You can the declare the params of the task as a Pair of ImageView and a String.

private class DownloadFilesTask extends AsyncTask< Pair<ImageView, String> >, Integer, Long> {

}
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54