dismiss()方法的进度对话框不起作用

时间:2012-11-14 15:25:44

标签: android progressdialog dismiss

可能会遇到同样的问题之前,我很抱歉,但我真的需要问这个。我正在尝试显示进度对话框,然后解雇它但是我无法做到这一点。我经常搜索并尝试了许多方法,但是真的无法通过。我从画廊挑选后上传图片。在上传过程中我想显示对话框,上传后的对话框应该被解雇,这里是我的代码。

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;
}

}

3 个答案:

答案 0 :(得分: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);
}
    }

答案 1 :(得分:1)

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();

      }
}

答案 2 :(得分:0)

您可以在此处查看您一直在寻找实施后台流程的想法:doInBackground not working in Android fragment