进度条对话框无法在android

时间:2015-06-03 06:40:50

标签: android-asynctask ftp progressdialog

在Asynctask中,进度条正在运行或文件正在成功上传。 我们在COMMENT1行之前写入时,COMMENT2行上传文件正确,但进度条不起作用。  但是如果我们在COMMENT1之后写COMMENT2然后我们面临ERROR代码500(语法错误)并且上传失败但是进度条  工作正常。

private class AsyncCaller extends AsyncTask<String, Integer, String> {

int bytesRead, bytesAvailable, bufferSize = 1024,progress;
 byte[] buffer;

 @Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog = new ProgressDialog(getActivity());
    mProgressDialog.setMessage("Uploading file..");
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setCancelable(false);
    mProgressDialog.setProgress(0);
    mProgressDialog.setMax(100);
    mProgressDialog.show();

}

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    // connection();
    FTPClient con = null;

      try
      {
          con = new FTPClient();
          con.connect(FTP_HOST);

          if (con.login(FTP_USER, FTP_PASS))
          {
              con.enterLocalActiveMode(); // important!
              con.setFileType(FTP.BINARY_FILE_TYPE);
              con.changeWorkingDirectory("/uploads/school-staging/files/");
              String data = FilePath;
              final DataOutputStream out = new DataOutputStream(con.getOutputStream());
              BufferedInputStream in = new BufferedInputStream(new FileInputStream(data));
              int bufferSize=1024;
              byte[] buffer = new byte[bufferSize];
              // Read file

              bytesRead = in.read(buffer, 0, bufferSize);//COMMENT 1
              progress=0;

              System.out.println("BYTE READ="+bytesRead);

              while (bytesRead > 0)
              {   

                  progress+=bytesRead;
                  System.out.println("PROGRESS="+progress);
                  out.write(buffer, 0, bytesRead);
                  bytesAvailable = in.available(); 
                  publishProgress((int)((progress*100)/(file.length())));
                  bufferSize = Math.min(bytesAvailable, bufferSize);

                  bytesRead = in.read(buffer, 0, bufferSize);

              }
            boolean result = con.storeFile(FileName, in);//COMMENT 2
              int code= con.getReplyCode();
                      System.out.println("CODE="+code);
              in.close();
              publishProgress(100); 

              if (result) Log.v("upload result", "succeeded");
              System.out.println("RESULT="+result);
              con.logout();
              con.disconnect();  
          }
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }

    return "ok";
}

@Override
 public void onProgressUpdate(Integer... values)
    {
        super.onProgressUpdate(values);
        mProgressDialog.setProgress(values[0]);
    }//end of onProgressUpdate


@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    connection();
    mProgressDialog.dismiss();
}

1 个答案:

答案 0 :(得分:0)

我遇到过类似的问题。原因似乎是onPreExecute()中的ui代码不能通过android系统进行渲染,而doInBackground()中的代码占用了cpu。您可以在onPostExecute()中关闭对话框,但是......

您必须在创建AsyncCaller之前显示进度对话框,将进度对话框传递给AsyncCaller,然后您可以在onPostExecute()中将其关闭。

    ProgressDialog pd = new ProgressDialog(getActivity());
    pd.setTitle("title string");
    pd.setIndeterminate(true);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.show();

    new AsyncCaller(getActivity(), pd).execute(params);

   in onPostExecute():
      if (pd != null && pd.isShowing())
            pd.dismiss();