0

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

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

}

4

3 回答 3

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);
}
    }
于 2012-11-14T15:37:48.153 回答
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();

      }
}
于 2012-11-14T21:04:33.717 回答
0

You can look here for the idea you've been looking for implementing a background process: doInBackground not working in Android fragment

于 2012-11-14T15:55:29.747 回答