简体   繁体   English

向storefile()添加进度对话框;

[英]adding progress dialog to storefile();

all is it possible to create a progress dialog to show the upload progress under a thread i use this code to upload a file called index.html to ftp. 都有可能创建一个进度对话框以显示线程下的上传进度,我使用此代码将名为index.html的文件上传到ftp。 please help me thanx in advance.. 请提前帮助我。

new Thread(new Runnable() {

  public void run() {
    Looper.prepare();

    FTPClient client = new FTPClient();
    try {  
      boolean  result = false;
      FileInputStream fis = null;
      client.connect(server);
      client.enterLocalPassiveMode();
      client.login(user, pass);
      client.makeDirectory("/public_html/"+str);
      client.setFileType(FTP.BINARY_FILE_TYPE);
      client.setFileTransferMode(FTP.BINARY_FILE_TYPE );
      client.changeWorkingDirectory(str);
      String path1 =      Environment.getExternalStorageDirectory() + "/index.htm";
      File f = new File(path1);
      String testname = "/public_html/"+str+"/"+f.getName();

      fis = new 
          FileInputStream(f);
      result = client.storeFile(testname, fis);


      if (result == true){
        Log.v("upload","upload successfull");

      }
      else{
        Log.v("upload", "upload failed");

      }
      client.logout();
      client.disconnect();
    } 
    catch (Exception e) {
      Context context = getApplicationContext();
      CharSequence text = "failed!!";
      int duration = Toast.LENGTH_SHORT;

      Toast toast = Toast.makeText(context, text, duration);
      toast.show();
    }
  }


}).start();

Why not use an asynctask? 为什么不使用asynctask? With it you could spawn a dialog in the onPreExecute method than in the onProgressUpdate method update the progress of the background task...There are other ways of doing it but i believe this to be the cleanest and easiest 有了它,您可以在onPreExecute方法中生成一个对话框,而不是在onProgressUpdate方法中更新后台任务的进度...还有其他方法可以做到,但是我相信这是最干净,最简单的方法

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
         // Escape early if cancel() is called
         if (isCancelled()) break;
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

The android dev reference should help clear things up http://developer.android.com/reference/android/os/AsyncTask.html android dev参考应该可以帮助您清除http://developer.android.com/reference/android/os/AsyncTask.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM