我正在通过 SQLCipher 实现对 SQLite 数据库的访问在我的 Android 混合 Cordova 应用程序中,它使用一个自定义插件(即由我编写)。 SQLCipher 文档以及其他有关在 Android 中使用 SQLite 的教程一直引用 Context.getDatabasePath
。在我的插件代码中,我存储了其他应用程序文件并广泛使用了 Context.getFilesDir
。 getDatabasePath
与 getFilesDir
有何不同。例如,由于操作系统决定通过删除存储在 Context.getFilesDir
中的一些文件来创建“更多空间”,它是否 promise 数据库将持久存在并且不会以某种方式被转储的更好机会?
最佳答案
两者都解析到同一个目录。 getDatabasePath
调用 getDatabasesDir
。
private File getDatabasesDir() {
synchronized (mSync) {
if (mDatabasesDir == null) {
if ("android".equals(getPackageName())) {
mDatabasesDir = new File("/data/system");
} else {
mDatabasesDir = new File(getDataDir(), "databases");
}
}
return ensurePrivateDirExists(mDatabasesDir);
}
}
@Override
public File getFilesDir() {
synchronized (mSync) {
if (mFilesDir == null) {
mFilesDir = new File(getDataDir(), "files");
}
return ensurePrivateDirExists(mFilesDir);
}
}
注意返回的 File
在这两种方法中都由 ensurePrivateDirExists
解析,它具有由 getDataDir
解析的相同输入目录。
Returns the absolute path to the directory on the filesystem where all private files belonging to this app are stored.
因此,您的情况没有区别。
不要忘记返回的路径可以改变,如doc说:
The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.
关于android - getFilesDir() 与 getDatabasePath(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48744395/