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