【发布时间】:2018-04-15 19:40:05
【问题描述】:
您好,我必须在我的异步任务中显示一个进度对话框,我的异步任务是:
private class UserProfileDialog extends AsyncTask<String, String, String> {
private String imagePath;
private String name;
private String status;
private String number;
private Activity activity;
private ProgressDialog progressDialog;
public UserProfileDialog(Activity activity) {
this.activity = activity;
}
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Please Wait...");
progressDialog.setCancelable(false);
progressDialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
imagePath = params[0];
name = params[1];
status = params[2];
number = params[3];
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
//show the dialog for dob date picker
final Dialog profileDialog = new Dialog(context);
profileDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
profileDialog.setContentView(R.layout.profile_dialog_me);
TextView title = (TextView) profileDialog.findViewById(R.id.title);
title.setTypeface(EasyFonts.robotoBold(context));
ImageView imageCloser = (ImageView) profileDialog.findViewById(R.id.imageCloser);
final ImageView profilePicture_x = (ImageView) profileDialog.findViewById(R.id.profilePicture);
//set the profile picture
Bitmap originalBitmap = BitmapFactory.decodeFile(new File(imagePath).getAbsolutePath());
originalBitmap = ModifyOrientationUtility.modifyOrientation(originalBitmap, new File(imagePath).getAbsolutePath());
Bitmap bitmap = resizeBitmapFitXY(400, 400, originalBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Glide.with(context)
.load(stream.toByteArray())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.dontAnimate()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
// TODO Auto-generated method stub
profilePicture_x.setImageBitmap(arg0);
}
});
TextView profileName = (TextView) profileDialog.findViewById(R.id.profileName);
profileName.setTypeface(EasyFonts.robotoLight(context));
profileName.setText(name);
TextView profileNumber = (TextView) profileDialog.findViewById(R.id.profileNumber);
profileNumber.setTypeface(EasyFonts.robotoLight(context));
profileNumber.setText(number);
TextView profileStatus = (TextView) profileDialog.findViewById(R.id.profileStatus);
profileStatus.setTypeface(EasyFonts.robotoLight(context));
profileStatus.setText(status);
//UI Events
imageCloser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
profileDialog.dismiss();
}
});
//show the dialog
profileDialog.show();
} catch (Exception exp) {
MyMLogger.showMLog("xm", "show profile dialog " + exp.toString(), logFromClass, "showProfileDialog");
exp.printStackTrace();
}
}
});
return null;
}
@Override
protected void onPostExecute(String s) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPostExecute(s);
}
}
AsyncTask 正在查找,但当我单击按钮开始我的任务时,它没有显示进度对话框!
但每当我关闭配置文件对话框时,它就会开始向我显示进度对话框(忽略 onPostExecute)。
有人可以解决这个问题吗,我有点困惑为什么会这样!
【问题讨论】:
-
你为什么在那里使用
AsyncTask?无论如何,您都在 UI 线程上运行所有内容。另外,为什么不让 Glide 处理图像大小调整? -
问题是我在 RecyclerView Adapter 中显示这个,并且在 Glide 中设置之前我正在修改图像方向!
-
我相信 Glide 也可以处理旋转。
标签: android android-asynctask progressdialog