【发布时间】:2017-11-27 09:31:47
【问题描述】:
我想在我的手机上备份我的偏好,在离线模式下,保证一旦卸载应用程序,这个偏好就会保留!!
如果有人可以给我一个好的教程或明确的步骤,我会感谢你!
【问题讨论】:
我想在我的手机上备份我的偏好,在离线模式下,保证一旦卸载应用程序,这个偏好就会保留!!
如果有人可以给我一个好的教程或明确的步骤,我会感谢你!
【问题讨论】:
抱歉,这是不可能的。您可以在卸载后写入文件的唯一位置是用户可以删除这些文件的位置。
【讨论】:
https://stackoverflow.com/a/11135800/3750554
重申上述链接所说的内容,您可以使用以下代码在 Android 上复制文件夹:
// If targetLocation does not exist, it will be created.
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
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 {
// make sure the directory we plan to store the recording in exists
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);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
我假设您希望备份应用程序。您可以将其在/data/data 中的位置复制到您选择备份的任何位置。
或者,如果您有终端模拟器(如果没有,Google Play 商店中有几个),您可以使用 POSIX 方式并使用cp。
cp -r /data/data/<app> <backup location>
但是,CommonsWare 是对的。无论您将内容复制到何处,用户都可以删除它们。
【讨论】: