2013-01-04 56 views
0

我用下面的代碼創建我的應用程序文件夾結構的備份(備份到遠程USB)添加進度條到備份

它工作正常,但現在我試圖找出如何說明當前的百分比是如何變化的。實際上,我想我不明白副本的工作方式是否足以列出文件夾中有多少文件來計算出百分比?或者增加什麼。

任何提示真的將不勝感激。

這裏是我的備用碼:

public void doBackup(View view) throws IOException{ 

     Time today = new Time(Time.getCurrentTimezone()); 
     today.setToNow(); 

     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
     final String curDate = sdf.format(new Date()); 

     final ProgressDialog pd = new ProgressDialog(this); 
     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setMessage("Running backup. Do not unplug drive"); 
     pd.setIndeterminate(true); 
     pd.setCancelable(false); 
     pd.show(); 
     Thread mThread = new Thread() { 
     @Override 
     public void run() { 
     File source = new File(Global.SDcard); 
     File dest = new File(Global.BackupDir + curDate); 
     try { 
      copyDirectory(source, dest); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
     pd.dismiss(); 


     } 
     }; 
     mThread.start(); 

    } 

    public void copyDirectory(File sourceLocation , File targetLocation) 
      throws IOException { 

       Log.e("Backup", "Starting backup"); 
       if (sourceLocation.isDirectory()) { 
        if (!targetLocation.exists() && !targetLocation.mkdirs()) { 
         throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath()); 
        } 

        String[] children = sourceLocation.list(); 
        for (int i=0; i<children.length; i++) { 
         copyDirectory(new File(sourceLocation, children[i]), 
           new File(targetLocation, children[i])); 
        } 
       } else { 

        Log.e("Backup", "Creating backup directory"); 
        File directory = targetLocation.getParentFile(); 
        if (directory != null && !directory.exists() && !directory.mkdirs()) { 
         throw new IOException("Cannot create dir " + directory.getAbsolutePath()); 
        } 

        InputStream in = new FileInputStream(sourceLocation); 
        OutputStream out = new FileOutputStream(targetLocation); 

        byte[] buf = new byte[1024]; 
        int len; 
        while ((len = in.read(buf)) > 0) { 
         out.write(buf, 0, len); 
        } 
        in.close(); 
        out.close(); 
        Log.e("Backup", "Finished"); 
       } 
      } 

回答

0

你可以把在最頂層File下面的函數來獲得總規模的內容...

​​

,然後跟蹤已複製文件的總大小。使用SizeOfCopiedFiles/SizeOfDirectory應該給你一個粗略的進度估計。

編輯:更新進度條...

下面的循環似乎是個不錯的地方做更新...

while ((len = in.read(buf)) > 0) { 
    out.write(buf, 0, len); 
    sizeOfCopiedFiles += len; 
    pd.setProgress((float)SizeOfCopiedFiles/SizeOfDirectory); 
} 

(注意,我假設有pd.setProgress (float f)取值爲0到1.)

爲此,您的copyDirectory(...)需要引用ProgressDialog,它也需要採用SizeOfCopiedFiles(來自以前調用的文件寫入)和SizeOfDirectory。函數需要返回sizeOfCopiedFiles的更新值以反映每次遞歸調用後的更新值。

最後,你有這樣的事情......(注:僞代碼爲清楚起見)

public long copyDirectory(File source, File target, long sizeOfCopiedFiles, 
     long sizeOfDirectory, ProgressDialog pd) { 

    if (source.isDirectory()) { 
     for (int i = 0; i < children.length; i++) { 
      sizeOfCopiedFiles = copyDirectory(sourceChild, destChild, 
        sizeOfCopiedFiles, sizeOfDirectory, pd); 
     } 
    } else { 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
      sizeOfCopiedFiles += len; 
      pd.setProgress((float)sizeOfCopiedFiles/sizeOfDirectory); 
     } 

    } 
    return sizeOfCopiedFiles; 
} 
+0

非常感謝這一點。我很抱歉,如果我是一個白癡,但我不知道在哪裏增加進度欄? – TMB87

+0

編輯答案。 – Mike