i have a default.realm file in my "assets/default.realm" folder, I am not able to make it as a default realm database
realm.getDefaultInstance();
src= new File("assets/default.realm");
dst=new File("/data/data/" + context.getPackageName() + "/files/");
if (!(realm.isEmpty())) {
Log.v("DB","already there!!");
} else {
try {
copyFile(src,dst);
} catch (IOException e) {
Log.v("DB","Wrong Path!");
}
}
void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
but failed to copy please help
Instead of manually copy the Realm file, you can add it to your RealmConfiguration
: https://realm.io/docs/java/1.1.1/api/io/realm/RealmConfiguration.Builder.html#assetFile-android.content.Context-java.lang.String-
Your Realm file might differ from your classes, and MigrationIsNeeded
exception will be thrown. In that case, you will have to write a migration step: https://realm.io/docs/java/latest/#migrations
So you will end up with something like:
RealmMigration migration = new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
schema.create("Person")
.addField("name", String.class)
.addField("age", int.class);
oldVersion++;
}
}
};
RealmConfiguration config = new RealmConfiguration.Builder(this)
.name(Realm.DEFAULT_REALM_NAME)
.migration(migration)
.assetFile(this,"Default.realm")
.schemaVersion(1)
.build();