目前,我有一个类来下载 Asynctask 的图像扩展。我想实现一个小的进度条来向用户显示在该空间中加载的图像。我试图将 Asynctask 类放在类活动中。现在的问题是调用类我需要类是静态的。但是如果我把静态类我不能控制进度对话框。
我的班级下载图片:
public class DownloadImages extends Activity{
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
// progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
public DownloadImage(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
Log.e("TAG","IMAGEN CARGADA");
}
// protected void onPreExecute(){
// }
}
}
要创建 ProgressBar,我认为我需要将类作为 Activity:
progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
这是我为我的问题创建的解决方案:
public class DownloadImages extends Activity{
ProgressBar progressBar = new ProgressBar(DownloadImages.this, null, android.R.attr.progressBarStyleSmall);
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
Log.e("TAG","IMAGEN CARGADA");
}
protected void onPreExecute(){
progressBar.setVisibility(View.VISIBLE);
}
}
}
现在的问题是如果我调用DownloadImage,这个类必须是静态的,但是如果我做静态我不能使用progressBar,除非它被声明为静态,如果我认为静态我不能用我的活动创建它。
有什么解决办法吗?
谢谢,对不起我的英语不好