我正在使用 AsyncTask 类下载测验应用程序的图像。这是类(class):
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);
downloaded_image = new BitmapDrawable(result);
//setting question image
question_view.setCompoundDrawablesWithIntrinsicBounds(null, null, null, downloaded_image);
}
private Bitmap download_Image(String url) {
//---------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
//--------------------------------------------------
}
}
图像正在下载,我将图像放入我的Question TextView中,如下所示:
downloaded_image = new BitmapDrawable(result);
//setting question image
question_view.setCompoundDrawablesWithIntrinsicBounds(null, null, null, downloaded_image);
我正在从我的 Activity 中调用此类,如下所示:
//image download
String imageURL = "http://cache.daylife.com/imageserve/074efG3gPV4oK/50x50.jpg";
image_downloadView.setTag(imageURL);
new DownloadImagesTask().execute(image_downloadView);
我想传递一些额外的参数,以便确定我是否正在下载问题或答案 TextView 的图像。
如何实现这一目标?
提前致谢!
附注:对于每组问题,我在申请中都有一个问题和四个选项。
最佳答案
您可以将参数传递给 AsyncTask 构造函数。
DownLoadImagesTask dt= new DownLoadImagesTask("hello",1); //passing string and integer value
dt.execute(image_downloadView);
在异步任务中
String s;
int value;
public DownLoadImagesTask(String string, int i) {
// TODO Auto-generated constructor stub
s=string;
value=1;
}
关于java - 使用附加参数调用 AsyncTask 类以检查在何处显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15612400/