2012-11-14 59 views
0

可能以前遇到同樣的問題,我很抱歉,但我真的需要問這個。我試圖顯示進度對話框,然後解僱它但我是無法做到。我搜查了很多,嘗試了很多方法,但無法通過。我從圖庫中挑選圖片後上傳圖片。並在上傳期間我想顯示對話框,並在上傳對話框後應該被駁回這裏是我的代碼。進度對話框的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; 
} 

}

+1

不能從裏面駁回對話框onActivityResult嘗試runonuithread –

+0

上運行的線程上傳或使用asynctask。您可以使用onPreExecute()顯示進度對話框,在doInBackground()中上傳工作,並在上傳完成後關閉onPostExecute()中的進度對話框。 – Raghunandan

+0

同意。你不應該在主線程上運行任何類似的東西。 – Phix

回答

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); 
} 
    } 
+0

謝謝。在哪裏運行這個asynTask類?我應該使用asynTask調用execute()方法來執行此操作嗎? –

+0

如何運行這個類?我是android新手,我不知道如何使用AsynTask。所以,請引導我。謝謝 –

+0

我在下面寫了如何使用asycTask,更多信息請參考這個url; http://developer.android.com/reference/android/os/AsyncTask.html – Talha

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

     } 
}