繁体   English   中英

将进度条添加到备份

[英]Add progress bar to 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");
                }
            }

您可以在最顶层的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;
}

暂无
暂无

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

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