0

uploadPhoto を指定して、このコードの UI スレッドで ProgressDialog を取得する方法を理解するのに本当に苦労しており、ガイダンスをいただければ幸いです。

@Override
    /** Handle Upload a Photo **/
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

        // Get image
        if (resultCode == RESULT_OK) {

            // ProgressDialog dialog = ProgressDialog.show(this, "", "Uploading Photo...", true, false);

            switch(requestCode) { 

                // Take Photo
                case 4001:      
                    // Upload
                    uploadPhoto(Uri.fromFile(mImageFile));
                    break;

                // Select Photo
                case 5001:

                    // Get image 
                    Uri selectedImage = imageReturnedIntent.getData();

                    // Upload
                    uploadPhoto(selectedImage);
                    break;
            }

            // Dismiss
            // dialog.dismiss();            
        }
    }
4

2 に答える 2

3

次のように AsyncTask を使用します。

public class NetworkTask extends AsyncTask<Object , Void, Object> {
    Context context;
    boolean shouldContinue = true;
    public ProgressDialog dialog;
    String waitMessage = "Please wait, loading data...";
    public NetworkTask(Context con){
        this.context = con;     
    }
    public void setMessage(String msg){
        waitMessage = "Please wait, "+msg;
    }
    protected void onPreExecute(){
        shouldContinue  = ConnectionUtils.isNetworkAvailable(context);
        if(shouldContinue)
            dialog = ProgressDialog.show(context, null, waitMessage, true);
        else{
            Dialog.showToast(context, Constants.NETWORK_ERROR);
            return;
        }
    }
    @Override
    protected void onPostExecute(Object result){
        if(dialog != null ){
            if(dialog.isShowing())
            dialog.dismiss();
            dialog = null;
        }           
    }
    @Override
    protected Object doInBackground(Object... params){
        //do uploading and other tasks
    }
}

そしてあなたのActivity呼び出しでは、このように:

NetWorkTask task = new NetWorkTask(this); //Here you can pass other params
task.execute("");
于 2011-10-21T12:50:34.233 に答える
2

AsyncTask を使用する場合があります。写真のアップロード機能を非同期タスクのバックグラウンドに配置します。

実行前に進行状況ダイアログを開始します。

実行後の進行状況ダイアログを閉じる/キャンセルします。

UI スレッドでの実行後および実行前の実行。

private class uploadPhoto extends AsyncTask<Void, Void, Void>{

            private ProgressDialog dialog;
        protected void onPostExecute(Void dResult) {

                dialog.cancel();

        }

        protected void onPreExecute() {


            dialog = new ProgressDialog(Myactivity.this);
            dialog.setCancelable(true);
            dialog.setMessage("uploading...");
            dialog.show();

                }

        protected Void doInBackground(Void... params) {
            // call upload photo here.
        }

    }

asyncTask を呼び出すには

new uploadPhoto().execute();
于 2011-10-21T12:45:05.753 に答える