private void copyApkAndFiles() {
File destDir = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "Backup");
if (!destDir.exists()) {
destDir.mkdirs();
}
copyFolder(getActivity().getFilesDir().getParentFile().getAbsolutePath(), destDir.getAbsolutePath());
}
/**
* 复制文件夹及其中的文件
*
* @param oldPath String 源文件夹路径
* @param newPath String 目的文件夹路径
*/
public boolean copyFolder(String oldPath, String newPath) {
try {
File newFile = new File(newPath);
if (!newFile.exists()) {
if (!newFile.mkdirs()) {
return false;
}
}
File oldFile = new File(oldPath);
String[] files = oldFile.list();
File temp;
for (String file : files) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file);
} else {
temp = new File(oldPath + File.separator + file);
}
if (temp.isDirectory()) {
copyFolder(oldPath + File.separator + file, newPath + File.separator + file);
} else if (!temp.exists()) {
return false;
} else if (!temp.isFile()) {
return false;
} else if (!temp.canRead()) {
return false;
} else {
FileInputStream fileInputStream = new FileInputStream(temp);
FileOutputStream fileOutputStream = new FileOutputStream(newPath + File.separator + temp.getName());
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Android将私有目录下的文件拷贝至SD卡目录
最新推荐文章于 2025-01-22 21:18:19 发布
本文介绍了一种在Android设备上备份应用程序及其文件的方法。通过将应用数据从设备内部存储复制到外部SD卡上的“Backup”目录中,实现了应用的完整备份。此过程包括创建备份目录、检查目录存在并进行递归复制。
5463

被折叠的 条评论
为什么被折叠?



