Android FTPUpload进度条

时间:2016-03-11 12:49:28

标签: android ftp android-progressbar

我正在使用 org.apache.commons.net.ftp [FTPClient类]上传用户拍照时创建的一些目录。但是,我无法显示进度条来演示该操作。

我创建了一个新线程,如下所示:

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

我试图在里面调用一个进度对话框,但它没有显示出来。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分: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();