2016-03-11 110 views
1

我正在使用org.apache.commons.net.ftp [FTP客戶端類]上傳用戶拍照時創建的一些目錄。但是,我無法展示進度條來展示該操作。Android FTPUpload進度條

我犯了一個新的線程來如下稱呼它:

new Thread(new Runnable() { 
    public void run() { 

     String server = "IP"; 
     int port = 21; 
     String user = "USER"; 
     String pass = "PASSWORD"; 

     FTPClient ftpClient = new FTPClient(); 

     try { 
      ftpClient.connect(server, port); 
      ftpClient.login(user, pass); 

      ftpClient.enterLocalPassiveMode(); 

      String remoteDirPath = "/"; 
      String localDirPath = Environment.getExternalStorageDirectory().getPath() + "/PicturesAPP/"; 
      FTPUtil.uploadDirectory(ftpClient, remoteDirPath, localDirPath, ""); 
      ftpClient.logout(); 
      ftpClient.disconnect(); 

     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 
}).start(); 

我想一個進度對話框裏面調用它,但它不會出現。

有人可以幫助我嗎?

回答

0

使用的的AsyncTask:

private class YourAsyncTask extends AsyncTask<Void, Void, Void> { 
       private ProgressDialog pDialog; 
       String server = "IP"; 
       int port = 21; 
       String user = "USER"; 
       String pass = "PASSWORD"; 

      FTPClient ftpClient = new FTPClient(); 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        pDialog = new ProgressDialog(getActivity()); 
        pDialog.setMessage("Loading..."); 
        pDialog.show(); 

       } 

       @Override 
       protected Void doInBackground(Void... params) { 
        try { 
        ftpClient.connect(server, port); 
        ftpClient.login(user, pass); 

        ftpClient.enterLocalPassiveMode(); 

        String remoteDirPath = "/"; 
        String localDirPath = Environment.getExternalStorageDirectory().getPath() + "/PicturesAPP/"; 
        FTPUtil.uploadDirectory(ftpClient, remoteDirPath, localDirPath, ""); 
        ftpClient.logout(); 
        ftpClient.disconnect(); 

        } catch (IOException ex) { 
        ex.printStackTrace(); 
        } 
       } 

       @Override 
       protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 
       pDialog.dismiss(); 
       } 
     } 

然後:

new YourAsyncTask().execute(); 
+0

謝謝你,它的工作! – Tarcisiofl