删除没有RealConfiguration的Realm文件

时间:2018-03-31 02:11:28

标签: android realm

在我的项目中,我有LoginFragment(接受用户名和密码)PinFrgment接受PIN并确认该PIN并将其保存在Realm中。 当用户确认PIN时,我正在创建一个realmconfiguration并获取realm实例,如下所示。

 protected void initRealm(Context context, byte[] key) {
    try {
        Realm.init(context);
        RealmConfiguration config = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().encryptionKey(key).build();
        Realm.setDefaultConfiguration(config);
        realmInstance = Realm.getDefaultInstance();
        DoPayApplication.getInstance().setRealmException(false);
    } catch (RealmFileException realmFileException) {
        DoPayApplication.getInstance().setRealmException(true);
        Log.d(TAG, realmFileException.toString());
    }catch (Exception ex){
        Log.i(TAG, ex.toString());
        DoPayApplication.getInstance().setRealmException(true);
    }

}

在退出并忘记PIN时,我正在调用以下方法来关闭/重置领域。

  public static void closeRealm(Context context) {
    try{
        if (realmInstance != null && !realmInstance.isClosed()) {
            realmInstance.close();
            RealmConfiguration config = realmInstance.getConfiguration();
            realmInstance.deleteRealm(config);
        }
    }catch (Exception e){
        Log.d(TAG, e.getMessage());
    }
}

只要我在应用程序中,在注销时我能够closeRealm,因为realmInstance不为null。

如果应用已关闭,用户将返回该应用。当我打电话给closeRealm' realmInstance'为null,我无法获取要删除的realmconfiguration。

由于忘记了密码而无法生成加密密钥。有没有办法删除/重新创建没有密钥的真实文件。

任何建议都会非常有用。

由于 [R

1 个答案:

答案 0 :(得分:0)

您只需使用原生工具删除.realm文件:

// create blur kernel
float kdata[] = { 0.0625f, 0.125f, 0.0625f,   0.125f, 0.25f, 0.125f, 0.0625f, 0.125f, 0.0625f };
Mat kernel(3, 3, CV_32F, kdata);

// resize kernel to 9x9 to create a more blurred effect
resize(kernel, kernel, {9,9});

// output is white, whats going wrong?
filter2D(src, output, -1, kernel);

通过这种方式,您可以从资产或原始资源复制数据库转储,也可以不使用域配置:

// realm file default name is default.ream
File file = new File(context.getFilesDir(), "realmFileName.realm");
file.delete();

您可以在How to find my realm file?

了解有关领域数据库文件的更多信息