我正在从URL加载图像视图,并希望在等待图像加载时显示进度对话框,当我运行应用程序时,porgress对话显示半秒然后消失并且没有任何关系图像的状态。我怎样才能使进度对话框仅在图像加载后消失?
下面是我的代码
//below is the download image task****************************************
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(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);
}
}
// above is the download imagetask ****************************************************
public class MyTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress;
public MyTask(ProgressDialog progress) {
this.progress = progress;
}
public void onPreExecute() {
progress.show();
}
public void onPostExecute(Void unused) {
progress.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
new DownloadImageTask((ImageView) findViewById(R.id.ivget))
.execute("http://dieboltdesigns.files.wordpress.com/2011/07/tom-brady-beast-poster-copy.png");
//above makes the progress bar
return null;
}
}
如果您需要doinbackgound方法/ asynctask,请在评论中告诉我
由于
答案 0 :(得分:3)
问题是如果我没有错,你在AsyncTask
内使用了AsyncTask
。 DownloadImageTask
是另一个独立于当前线程的线程。那就是问题所在。
只需在DownloadImageTask
中记下MyTask
的所有代码。
修改强>
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
ProgressDialog pd;
Context mContext;
public DownloadImageTask(Context context,ImageView bmImage) {
this.bmImage = bmImage;
mContext = context;
}
public void onPreExecute() {
pd = ProgressDialog.show(mContext, "dialog title","dialog message", true);
}
public void onPostExecute(Bitmap result) {
pd.dismiss();
if(result!=null)
bmImage.setImageBitmap(result);
}
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;
}
}