【问题标题】:dismiss() method of Progress dialog is not working进度对话框的dismiss() 方法不起作用
【发布时间】:2012-11-15 05:04:33
【问题描述】:

您之前可能遇到过同样的问题,对此我很抱歉,但我真的需要问这个问题。我正在尝试显示进度对话框,然后将其关闭但我无法做到。我已经搜索了很多并尝试了很多方法,但无法真正通过。我从图库中挑选后上传图片。在上传期间我想显示对话框,上传后对话框应该被关闭这是我的代码。

public class FaceActivity extends Activity {
 private static int RESULT_LOAD_IMAGE = 1;
 private Button upbtn;
 public Bitmap bm;
 public ByteArrayOutputStream bos;
 public byte[] bitmapdata;
 public String picturePath;
 private ProgressDialog pd;
 private BitmapFactory.Options options;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_face);


    //pd = new ProgressDialog(FaceActivity.this);

    upbtn = (Button) findViewById(R.id.buttonLoadPicture);
    upbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);



        }
    });
}
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                picturePath = cursor.getString(columnIndex);
                cursor.close();

                options = new BitmapFactory.Options();
             // will results in a much smaller image than the original
                options.inSampleSize = 8;

                upload();



            }

            }



    public void upload(){

   // Here I am showing the dialog 
        pd = ProgressDialog.show(FaceActivity.this, "Please Wait", "Loading...", true, false);
        bm = BitmapFactory.decodeFile(picturePath);
        bos = new ByteArrayOutputStream(); 
        bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
        bitmapdata = bos.toByteArray();

        ParseFile file = new ParseFile("pic.jpg", bitmapdata);
        file.saveInBackground();

        ParseObject po = new ParseObject("Images");       
        po.put("Images", file);
        po.saveInBackground();

        ImageView imageView = (ImageView) findViewById(R.id.targetimage);

        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath,options));
// want to dismiss dialog here 
        pd.dismiss();
        Toast.makeText(this, "Image Uploaded Successfully", Toast.LENGTH_LONG).show();
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_face, menu);
    return true;
}

}

【问题讨论】:

  • 您不能从 onActivityResult 内部关闭对话框尝试 runonuithread
  • 在线程上运行上传或使用异步任务。您可以使用 onPreExecute() 显示进度对话框,在 doInBackground() 中上传工作,并在上传完成后在 onPostExecute() 中关闭进度对话框。
  • 同意。你不应该在主线程上运行这样的东西。
  • @Kenny 我将如何使用 runonuiThread 执行此操作?

标签: android progressdialog dismiss


【解决方案1】:

尝试在 asyc 任务中执行此操作。

private class asynUpload extends AsyncTask<String, Void, Integer> {
protected Integer doInBackground(String... params) {
try {
     runOnUiThread(new Runnable() {
            public void run() {
                pd = ProgressDialog.show(FaceActivity.this, "Please Wait", "Loading...", true, false);
        }
       });

bm = BitmapFactory.decodeFile(picturePath);
    bos = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
    bitmapdata = bos.toByteArray();

    ParseFile file = new ParseFile("pic.jpg", bitmapdata);
    file.saveInBackground();

    ParseObject po = new ParseObject("Images");       
    po.put("Images", file);
    po.saveInBackground();

    ImageView imageView = (ImageView) findViewById(R.id.targetimage);

    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath,options));

} catch (Exception e) {             
        return 0;
}
return 1;
}

protected void onPostExecute(Integer result) {
     try {
              runOnUiThread(new Runnable() {
        public void run() {
                       pd.dismiss();
        }
       });

    } catch (Exception e) {}

      super.onPostExecute(result);
}
    }

【讨论】:

  • 谢谢。在哪里运行这个 asynTask 类?我应该使用 asynTask 调用 execute() 方法吗?
  • 如何运行这个类?我是 android 新手,我不知道如何使用 AsynTask。所以,请指导我。谢谢
  • 我在下面写了如何使用 asycTask ,有关更多信息,请参阅此网址; developer.android.com/reference/android/os/AsyncTask.html
  • 成功了。谢谢。以后我会记得使用 AsyncTask 来完成这类工作。再次感谢
【解决方案2】:
protected void onActivityResult(int requestCode, int resultCode, Intent data) 

{

   super.onActivityResult(requestCode, resultCode, data);


   if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 

      {

         Uri selectedImage = data.getData();

         String[] filePathColumn = { MediaStore.Images.Media.DATA };

         Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
         cursor.moveToFirst();

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         picturePath = cursor.getString(columnIndex);
         cursor.close();

          options = new BitmapFactory.Options();
          // will results in a much smaller image than the original
          options.inSampleSize = 8;

          // use the task here 
          new asynUpload().execute();

      }
}

【讨论】:

    【解决方案3】:

    你可以在这里寻找你一直在寻找的实现后台进程的想法:doInBackground not working in Android fragment

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      相关资源
      最近更新 更多