可能以前遇到同樣的問題,我很抱歉,但我真的需要問這個。我試圖顯示進度對話框,然後解僱它但我是無法做到。我搜查了很多,嘗試了很多方法,但無法通過。我從圖庫中挑選圖片後上傳圖片。並在上傳期間我想顯示對話框,並在上傳對話框後應該被駁回這裏是我的代碼。進度對話框的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;
}
}
不能從裏面駁回對話框onActivityResult嘗試runonuithread –
上運行的線程上傳或使用asynctask。您可以使用onPreExecute()顯示進度對話框,在doInBackground()中上傳工作,並在上傳完成後關閉onPostExecute()中的進度對話框。 – Raghunandan
同意。你不應該在主線程上運行任何類似的東西。 – Phix