通常我使用Realm作为:
RealmConfiguration config = new RealmConfiguration.Builder(applicationContext).deleteRealmIfMigrationNeeded().build();
如何将包含数据的数据库添加到项目的assets文件夹中并读取它?
通常我使用Realm作为:
RealmConfiguration config = new RealmConfiguration.Builder(applicationContext).deleteRealmIfMigrationNeeded().build();
如何将包含数据的数据库添加到项目的assets文件夹中并读取它?
自从Realm Java 0.91.0出现以来 assetFile(String)
关于的选项 RealmConfiguration
自动将从资产中复制文件并在需要时使用该文件(例如,如果Realm第一次打开或由于某种原因被删除):
RealmConfiguration config = new RealmConfiguration.Builder()
.assetFile("path/to/file/in/assets") // e.g "default.realm" or "lib/data.realm"
.deleteRealmIfMigrationNeeded()
.build()
以上将在第一次打开Realm时从资产中复制文件,或者由于迁移而被删除(请记住在这种情况下更新资产Realm)。
老答案:
可以将一个Realm数据库捆绑在assets文件夹中,但是只需要在第一次启动应用程序时从那里复制它。
copyBundledRealmFile(this.getResources().openRawResource(R.raw.default_realm), "default.realm");
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 Java 0.91.0出现以来 assetFile(String)
关于的选项 RealmConfiguration
自动将从资产中复制文件并在需要时使用该文件(例如,如果Realm第一次打开或由于某种原因被删除):
RealmConfiguration config = new RealmConfiguration.Builder()
.assetFile("path/to/file/in/assets") // e.g "default.realm" or "lib/data.realm"
.deleteRealmIfMigrationNeeded()
.build()
以上将在第一次打开Realm时从资产中复制文件,或者由于迁移而被删除(请记住在这种情况下更新资产Realm)。
老答案:
可以将一个Realm数据库捆绑在assets文件夹中,但是只需要在第一次启动应用程序时从那里复制它。
copyBundledRealmFile(this.getResources().openRawResource(R.raw.default_realm), "default.realm");
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;
}
自从境界 0.89.0
RealmConfiguration.initialData(Realm.Transaction)
现在可用于在Realm文件首次使用之前填充它。
RealmConfiguration conf = new RealmConfiguration.Builder(context)
.initialData(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.beginTransaction();
realm.createObject(....)
realm.commitTransaction();
}
}).deleteRealmIfMigrationNeeded().name("mRealm.db").build();
Realm realm = Realm.getInstance(conf);
我们有类似的需求,并且还希望支持与iOS版本的应用程序共享的只读领域数据库。
我们创建了一个简单的库并开源了它。它包括@ christian-melchior的答案中给出的复制代码,以及与APK捆绑在一起的只读领域数据库的一些可选的额外跟踪。评论和公关欢迎。看到:
Realm在其中有一个特殊参数 RealmConfiguration.Builder
称为assetFile。您可以使用它:
realmConfiguration = new RealmConfiguration.Builder()
.assetFile("dataBase/default.realm") // your app's packaged DB
...
.build();
只需设置你的资产数据库路径和文件名,你就可以不用任何android-realm-asset-helper lib或copy-file-from-assets代码。在这个例子中,我的app打包的DB文件位于“assets / dataBase / default.realm”中。
注意,2以下的版本有另一种调用assetFile的方法,你应该另外传递上下文:
realmConfiguration = new RealmConfiguration.Builder(context)
.assetFile(context, "dataBase/default.realm")
.build();
您可以使用 assetFile()
方法。请注意,您不能使用 assetFile()
同 deleteIfMigrationNeeded()
。