如何在Android硬盘上而不是SD卡上创建持久文本文件
在阅读了7年来关于如何从Android设备获取唯一设备id的问题(以及人们应该如何重新考虑不这样做)后,我认为在设备上创建一个文件(不是在SD卡上,也不是在卸载或用户擦除数据时被擦除的位置)是一个很好的方法 但是如何保存这个文件,这可能吗 我有此代码,但它将只保存到应用程序内部存储目录如何在Android硬盘上而不是SD卡上创建持久文本文件,android,filesystems,uniqueidentifier,Android,Filesystems,Uniqueidentifier,在阅读了7年来关于如何从Android设备获取唯一设备id的问题(以及人们应该如何重新考虑不这样做)后,我认为在设备上创建一个文件(不是在SD卡上,也不是在卸载或用户擦除数据时被擦除的位置)是一个很好的方法 但是如何保存这个文件,这可能吗 我有此代码,但它将只保存到应用程序内部存储目录 public synchronized static String applicationId() { if (sID == null) { File file = new File(Ap
public synchronized static String applicationId() {
if (sID == null) {
File file = new File(Application.getInstance().getFilesDir(), Application.getInstance().getString(R.string.application_id_key));
try {
if (!file.exists())
writeApplicationIdFile(file);
sID = readApplicationIdFile(file);
} catch (Exception e) {
throw new RuntimeException("Writing ApplicationIdFile failed",e);
}
}
return sID;
}
private static String readApplicationIdFile(File file) throws IOException {
RandomAccessFile in = new RandomAccessFile(file, "r");
byte[] bytes = new byte[(int) in.length()];
in.readFully(bytes);
in.close();
return new String(bytes);
}
private static void writeApplicationIdFile(File file) throws IOException {
FileOutputStream out = new FileOutputStream(file);
out.write(UUID.randomUUID().toString().getBytes());
out.close();
LogManager.i("writeApplicationIdFile", "Writing ApplicationIdFile");
}
如果您具有root访问权限,may
/data/local/tmp
可能会对您有所帮助。
通常情况下,这并不是移动应用程序存储唯一ID的好解决方案。嗯,这太糟糕了