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");
                }
            }
4

1 回答 1

0

您可以在最顶层调用以下函数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;
}
于 2013-01-04T10:57:28.070 回答