Java 如何通过FileinputStream添加还原设置功能
因此,我是编程新手,我在我的应用程序中添加了一项功能,使用fileoutputStream将所有SharedReference密钥值保存在设备内部数据文件夹中,就像这样在map中添加所有数据并存储在jsonobject中Java 如何通过FileinputStream添加还原设置功能,java,android,fileinputstream,xposed,xposed-framework,Java,Android,Fileinputstream,Xposed,Xposed Framework,因此,我是编程新手,我在我的应用程序中添加了一项功能,使用fileoutputStream将所有SharedReference密钥值保存在设备内部数据文件夹中,就像这样在map中添加所有数据并存储在jsonobject中 private String maptojson(){ Map<String, ?> map = prf.getAll(); JSONObject object = new JSONObject(); for (Map.Entry<St
private String maptojson(){
Map<String, ?> map = prf.getAll();
JSONObject object = new JSONObject();
for (Map.Entry<String,?> entry : map.entrySet()){
try {
object.put( entry.getKey(), entry.getValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
return object.toString();
}
现在我想通过使用文件“MyAppSetting.ma”添加还原设置意味着还原设置。我知道我可以用
FileInputStream
来做,但我不知道怎么做?如果可以,请提供帮助您是否知道externalStorageDirectory
将为您提供物理SD卡的路径,而不是共享存储的路径(请参阅)。此外,请注意,写入共享存储和SD卡需要应用程序在AndroidManifest.xml
中具有适当的权限,并且应用程序必须在运行时请求这些权限。对于外部sd卡,您甚至必须在文件资源管理器中选择授予写入权限的路径。Lat但并非最不重要的是,对共享存储的直接文件访问仅适用于Android 9。对于Android 10+而言,您需要使用作用域存储。请考虑将数据绑定到<代码> /DATAB/TAMP/TMP/</代码>。所有应用程序都应该有写访问权限(但目录上没有读访问权限,因此您无法列出文件)。是的,我知道,但externalStorageDirectory现在为我工作,它将我的设置文件写入内部“data/MyApp/”中我的设置文件在该目录中成功创建,但问题是我想将该文件作为还原设置备份读取。我正在使用一个xposed模块,该模块可以更改设备参数,如imei、设备、,识别这些身份参数,为已安装的应用程序提供欺骗参数并使其正常工作,但现在我想添加一个功能来保存欺骗参数,当用户需要时,他们可以恢复这些参数,如果可能的话。你能贡献我的项目吗?你有类似于此应用程序的东西吗?我使用ObjectOutputStream和ObjectInputStream修复了它,谢谢您的帮助
public void backupSetting() {
Throwable th;
FileOutputStream fileOutputStream;
IOException e;
FileOutputStream fileOutputStream2 = null;
File externalStorageDirectory = Environment.getExternalStorageDirectory();
if (externalStorageDirectory.exists() && externalStorageDirectory.canWrite()) {
if (externalStorageDirectory.getUsableSpace() >= 1048576) {
File file = new File(externalStorageDirectory.toString() + "/data/MyApp/" + "MyAppSetting.ma");
try {
new File(file.getParent()).mkdirs();
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
try {
fileOutputStream.write(maptojson().getBytes());
fileOutputStream.flush();
fileOutputStream.close();
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
} catch (IOException e3) {
e = e3;
try {
e.printStackTrace();
if (fileOutputStream != null) {
}
} catch (Throwable th2) {
th = th2;
fileOutputStream2 = fileOutputStream;
if (fileOutputStream2 != null) {
try {
fileOutputStream2.close();
} catch (IOException e4) {
e4.printStackTrace();
throw th;
}
}
throw th;
}
}
} catch (IOException e5) {
e = e5;
fileOutputStream = null;
e.printStackTrace();
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e6) {
e6.printStackTrace();
}
}
} catch (Throwable th3) {
th = th3;
if (fileOutputStream2 != null) {
}
try {
throw th;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}