For export my realm file I am using the next code:
File exportRealmFile;
exportRealmFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "default.realm");
getRealm().writeCopyTo(exportRealmFile);
After that I am trying to import this file. I open Android Monitor and push file onto the another device.
But when I want to get RealmObject, for example, StatObject I see the object size is 0. But I know the size isn't 0.
RealmResults<StatObject> statObjects =
realmForThisThread.where(StatObject.class).findAll();
LOG.debug("Size "+statObjects.size());
I open Stetho and I do not see any objects! But before export I saw seven different realm objects. However, the library itself has the same size. What am I doing wrong?
I am trying to import realm file using:
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("Realmexport.realm")
.assetFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath())
.build();
// Realm.setDefaultConfiguration(realmConfiguration);
But nothing changed.
Import file:
File oldRealmFile = new File(getRealm().getPath());
File newFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) ,"default.realm");
FileOutputStream outputStream=null;
FileInputStream inputStream=null;
try {
outputStream = new FileOutputStream(oldRealmFile);
inputStream = new FileInputStream(newFile);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
outputStream.close();
inputStream.close();
}catch (IOException exc){
exc.printStackTrace();
}finally {
}
This approach helps to import file but new file I can't use it. Because I don't see my Objects.
You're using
assetFile
in a wrong way. You're referencing an internal device location.assetFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath())
whereas the exported Realm should reside in theassets
folder of your APK. Also specifying aname
will create a new (empty) Realm.Try the following configuration instead.
Your APK:
└── src └── main ├── AndroidManifest.xml ├── assets │ └── myExported.realm ├── java └── res