这是我用来在相机启动和拍照时实现 ProgressDialog 的代码。
public void onPhotoTaken() {
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(_path, options);
try {
ProgressDialog dialog = ProgressDialog.show(this, "Loading", "Please wait...", true);
ExifInterface exif = new ExifInterface(_path);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
单击我的 Android 设备的保存按钮时,它没有显示进度对话框,而是显示黑屏。请问哪里出了问题。
最佳答案
你应该记住 Progress dialog是你应该在一个单独的线程中运行它。如果您在 UI 线程中运行它,您将看不到任何对话框。
如果您不熟悉 Android Threading那么你应该了解AsyncTask .这可以帮助您实现 painless Threads .
示例代码:
private class CheckTypesTask extends AsyncTask<Void, Void, Void>{
ProgressDialog asyncDialog = new ProgressDialog(IncidentFormActivity.this);
String typeStatus;
@Override
protected void onPreExecute() {
//set message of the dialog
asyncDialog.setMessage(getString(R.string.loadingtype));
//show dialog
asyncDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
//don't touch dialog here it'll break the application
//do some lengthy stuff like calling login webservice
return null;
}
@Override
protected void onPostExecute(Void result) {
//hide the dialog
asyncDialog.dismiss();
super.onPostExecute(result);
}
}
祝你好运。
编辑
方法 show()
必须从用户界面 (UI) 线程调用,而 doInBackground()
运行在不同的线程上,这是 AsyncTask 的主要原因被设计出来。
您必须在 onProgressUpdate()
或 onPostExecute
中调用 show()
关于java - Android:进度对话框显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33868612/