为备份添加进度条



我使用以下代码在我的应用程序上创建一个文件夹结构的备份(备份到远程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");
                }
            }

您可以在最上面的File上调用以下函数来获取其内容的总大小…

long getFileSize(File aFile) {
    //Function passed a single file, return the file's length.
    if(!aFile.isDirectory())
        return aFile.length();
    //Function passed a directory.
    // Sum and return the size of the directory's contents, including subfolders.
    long netSize = 0;
    File[] files = aFile.listFiles();
    for (File f : files) {
        if (f.isDirectory())
            netSize += getFileSize(f);
        else
            netSize += f.length();
    }
    return netSize;
}

,然后跟踪已复制文件的总大小。使用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;
}

最新更新