我成功地将预定义的realm数据库添加到我的应用程序并获取数据
copyBundledRealmFile(this.getResources().openRawResource(R.raw.test),
"test");
RealmConfiguration config1 = new RealmConfiguration.Builder(this)
.name("test")
.schemaVersion(1)
.migration(new Migration())
.build();
realm = Realm.getInstance(config1);
realm.close();
private String copyBundledRealmFile(InputStream inputStream, String outFileName) {
try {
File file = new File(this.getFilesDir(), outFileName);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
outputStream.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
当我尝试使用
时如何获取领域实例的问题 realm = Realm.getDefaultInstance();
给我空数据 如何在应用程序中获取实例
答案 0 :(得分:4)
为了获取默认值,您必须设置默认值:
RealmConfiguration config1 = new RealmConfiguration.Builder(this)
.name("test")
.schemaVersion(1)
.migration(new Migration())
.build();
Realm.setDefaultConfiguration(config1); // <-- here
现在你可以用
来搞定Realm.getDefaultInstance();