从自定义对话框显示进度对话框
我试图在图像上传到服务器时从自定义对话框显示进度对话框。这就是我所遇到的:
final Dialog mDialog = new Dialog(this, R.style.CustomDialogTheme);
mDialog.setContentView(R.layout.sendimage_dialog);
ImageView im = (ImageView)mDialog.findViewById(R.id.image_to_upload);
im.setImageURI(Constants.currImageURI);
Button upload = (Button)mDialog.findViewById(R.id.upload);
upload.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
mDialog.dismiss();
});
mDialog.show();
mDialog.setOnDismissListener(new OnDismissListener(){
@Override
public void onDismiss(DialogInterface di) {
ProgressDialog progress = new ProgressDialog(CameraActivity.this, R.style.CustomDialogTheme);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setMessage("Please wait while your photo is being uploaded.");
progress.show();
String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI)));
try {
parseResponse(response);
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
}
progress.dismiss();
Toast.makeText(getApplicationContext(), "Using your computer, log into your graFighters account and your image will be waiting for you.", Toast.LENGTH_SHORT).show();
}
});
问题是,当我点击上传按钮时,我的进度对话框永远不会显示。有什么想法可能是为什么吗?
I am trying to show a progress dialog from a custom dialog while an image get uploaded to a server. Here is what I have:
final Dialog mDialog = new Dialog(this, R.style.CustomDialogTheme);
mDialog.setContentView(R.layout.sendimage_dialog);
ImageView im = (ImageView)mDialog.findViewById(R.id.image_to_upload);
im.setImageURI(Constants.currImageURI);
Button upload = (Button)mDialog.findViewById(R.id.upload);
upload.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
mDialog.dismiss();
});
mDialog.show();
mDialog.setOnDismissListener(new OnDismissListener(){
@Override
public void onDismiss(DialogInterface di) {
ProgressDialog progress = new ProgressDialog(CameraActivity.this, R.style.CustomDialogTheme);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setMessage("Please wait while your photo is being uploaded.");
progress.show();
String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI)));
try {
parseResponse(response);
} catch (JSONException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
}
progress.dismiss();
Toast.makeText(getApplicationContext(), "Using your computer, log into your graFighters account and your image will be waiting for you.", Toast.LENGTH_SHORT).show();
}
});
The problem is, when I hit the upload button, my progress dialog never shows. Any ideas why this might be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您在主 (UI) 线程中执行网络调用 (uploadUserPhoto),这意味着 UI 在调用 show() 和 dismiss() 之间不会更新。如果您希望此功能正常工作,您可以启动一个单独的工作线程来执行 uploadUserPhoto 操作。工作线程完成后,它必须向活动发出信号以删除对话框。
It is because you're performing the network call (uploadUserPhoto) in the main (UI) thread, which means that the UI won't be updated between the calls to show() and dismiss(). If you want this to work, you could launch a separate worker Thread that performs the uploadUserPhoto instead. After the worker Thread has finished, it must then signal the Activity to remove the dialog.
您正在单击方法中显示和关闭您的 ProgressDialog。至于
它应该被驳回。如果您创建了自定义对话框,我们可能需要查看更多内容才能获得更好的想法。
编辑:然后调用对话框以在 onClick 方法之外显示。
You are showing and dismissing your ProgressDialog in the click method. As for
It should be dismissing. If you created a custom dialog we may need to see more of it to get a better idea.
EDIT: and then your calling the dialog to show outside of your onClick method.
您应该使用
AlertDialog
。这是一个应该非常适合您的代码的示例:我还建议考虑使用 AsyncTask 和 ProgressDialog。
You should use an
AlertDialog
. Here is an example that should fit your code pretty well:I also recommend looking into using an AsyncTask and a ProgressDialog.