[英]how to create a persistent text file on Android hard drive and not on SD card
After reading 7 years of SO questions about how to get an unique device id from and Android device(and how one really should rethink not to do that), I think that creating a file on the device(not on SD card and not where it get wiped during uninstall or user wipe data) is a good approche. 在阅读了7年的SO问题之后,关于如何从Android设备获取唯一的设备ID(以及如何真正重新考虑不要这样做),我认为在设备上创建文件(不在SD卡上,而不是在文件所在的位置)最好在卸载过程中擦除或用户擦除数据)。
But how to save this file and is it possible? 但是如何保存此文件,这可能吗?
I have this code but it will save it to the app internal storage dir only 我有此代码,但它只会将其保存到应用程序内部存储目录中
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");
}
Might /data/local/tmp
could help to you if you have got root access. 如果您具有root用户访问权限,则
/data/local/tmp
可能会对您有所帮助。 In common practice this is not goot solution for mobile apps to store unique ID's. 通常,这不是移动应用程序存储唯一ID的理想解决方案。 Imho, this is awful.
恕我直言,这太糟糕了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.