gpt4 book ai didi

android - SharedPreferences 问题,覆盖

转载 作者:行者123 更新时间:2023-11-30 02:22:42 27 4
gpt4 key购买 nike

我有一些值存储在一个共享首选项文件中,在我的代码中的某个时刻,我拆分了这个文件并将共享上传到多个云,以便我可以有一个安全的备份。

所以我的应用程序现在开始时会查看共享首选项文件夹,如果该文件不存在,它会要求用户登录云端以恢复文件(想想用户丢失其文件的情况手机)。

因此,在尝试性能时,我模拟我没有这样的文件并下载各个部分并将它们组合起来以获得整体。该文件已完美恢复(我手动打开它,它包含所有字段)。

但是,当我尝试在这个文件中写入新值时,系统无法识别它,将其删除并创建一个新值。总之,文件被覆盖了。即使我尝试从旧文件(我下载的文件)中读取一个值,它也不起作用并返回默认值。

您知道可能是什么问题吗?除了不使用共享首选项之外是否还有其他解决方案?

KeyManagement.combineFile(split);
SharedPreferences prefs = mContext.getSharedPreferences("CACSPrefs", 0);
String key = prefs.getString("KEY", null);
Editor editor = prefs.edit();
editor.putString("IS_STORED", "TRUE");
editor.commit();

split 是一组文件(我从网上下载的部分)。我检查过重组文件名为“CACSPrefs.xml”

编辑

当我拆分原始文件时,我会一个一个地向每个共享发送一个字节,每个共享都使用相同的名称调用但添加“_1”、“_2”...所以合并文件的行为反过来。

protected static void combineFile(File[] fileShares) throws FileNotFoundException, IOException{

// create file
String parentPath = fileShares[0].getParent();
String fileName = fileShares[0].getName().substring(0,
fileShares[0].getName().indexOf("_"));
File file = new File(parentPath + "/" + fileName);
if (!file.exists()) file.createNewFile();
FileInputStream[] in = new FileInputStream[fileShares.length];

// create input streams
int length = 0;
for (int i = 0; i < fileShares.length; i++){
length += (int) fileShares[i].length();
in[i] = new FileInputStream(fileShares[i].getAbsolutePath());
}

// read byte by byte and write
FileOutputStream out = new FileOutputStream(file);
for (int i = 0; i < length; i++){
int b;
if((b = in[i % fileShares.length].read()) == -1) break;
out.write((byte) b);
}
out.flush();
out.close();

for (int i = 0; i < fileShares.length; i++){
in[i].close();
}
}

最佳答案

我会建议另一种解决方案。使用标准 SharedPreferences.get*() 方法编写一个方法,仅将您想要上传到云的值转换为 JSON,然后上传 JSON。然后,当您需要它时,您可以获得 JSON,读取值并使用标准的 SharedPreferences.Editor.put*(...) 方法再次存储它。

您可以避免原始代码可能引入的任何错误,这些错误会导致 SharedPreferences 无法识别文件(或不使用它来加载值),因为您使用的是访问共享的官方方式偏好。

编辑:或者不使用 JSON,您可以使用您喜欢的任何传输协议(protocol),甚至是您自己的传输协议(protocol),只要您按照上述方式编写首选项即可。

编辑 2:正如 OP 在此处要求的那样,提供了一个示例,说明如何将选定的共享首选项作为 JSON 获取,反之亦然。

要将一些共享偏好设置为 JSON 对象,请执行以下操作:

SharedPreferences prefs = getApplicationContext().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
JSONObject json = new JSONObject();

try {
json.put("my_long", prefs.getLong("my_long", 0));
json.put("my_string", prefs.getString("my_string", null));
// only upload this one if it's set
if (prefs.contains("my_boolean")) json.put("my_boolean", prefs.getBoolean("my_boolean", false));
} catch (JSONException e) {
e.printStackTrace();
}

String toUpload = json.toString();

现在上传 String toUpload 例如作为 POST 请求正文。

要保存从服务器获取的共享首选项,请执行以下操作:

String fromDownload =...; // obtained from http response body

try {
SharedPreferences prefs = getApplicationContext().getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
JSONObject json = new JSONObject(fromDownload);

SharedPreferences.Editor editor = prefs.edit();
// this will throw JSONException if no value came from server
editor.putLong("my_long", json.getInt("my_long"));
// this will set a default value if no value came from server
editor.putString("my_string", json.optString("my_string", "default"));
// this will only store the value if it came from server otherwise no-op
if (json.has("my_boolean")) editor.putBoolean("my_boolean", json.getBoolean("my_boolean"));
editor.apply();
} catch (JSONException e) {
e.printStackTrace();
}

关于android - SharedPreferences 问题,覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28262440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com