android - getFilesDir() 与 getDatabasePath()

标签 android sqlite android-context

我正在通过 SQLCipher 实现对 SQLite 数据库的访问在我的 Android 混合 Cordova 应用程序中,它使用一个自定义插件(即由我编写)。 SQLCipher 文档以及其他有关在 Android 中使用 SQLite 的教程一直引用 Context.getDatabasePath。在我的插件代码中,我存储了其他应用程序文件并广泛使用了 Context.getFilesDirgetDatabasePathgetFilesDir 有何不同。例如,由于操作系统决定通过删除存储在 Context.getFilesDir 中的一些文件来创建“更多空间”,它是否 promise 数据库将持久存在并且不会以某种方式被转储的更好机会?

最佳答案

两者都解析到同一个目录。 getDatabasePath 调用 getDatabasesDir

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);
        }
    }

getFilesDir :

  @Override
    public File getFilesDir() {
        synchronized (mSync) {
            if (mFilesDir == null) {
                mFilesDir = new File(getDataDir(), "files");
            }
            return ensurePrivateDirExists(mFilesDir);
        }
    }

注意返回的 File 在这两种方法中都由 ensurePrivateDirExists 解析,它具有由 getDataDir 解析的相同输入目录。

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/

相关文章:

Android 应用程序静音和取消静音 MediaPlayer

android - sqlite数据库不能使用cordova在一个表中插入两个以上的字段,

Android Activity 在旋转时刷新

android - 在我的 Android 设备中检测已安装的证书

android - 如何在android中传递一个带有嵌套参数的RPC Json

mysql - 带有 .new 的 Rails 控制台出错

android - 将值分配给不同类的 TextView

android - 如何在我自己的实用程序类中提供上下文来调用必要的函数

android - 是否可以在 Android 中使用具有 MVP 架构的分页库?

sqlite - React-native 的纯 JSON 与应用内数据库?