添加进度条进行备份

时间:2013-01-04 10:47:50

标签: java android backup

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

1 个答案:

答案 0 :(得分: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;
}