0

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.

Delphian
  • 1,650
  • 3
  • 15
  • 31
  • How are you importing the exported Realm? can you share the entire code? I suspect you're just opening a new Realm (hence the size 0) since you're not pushing the exported Realm in the correct location. Alternatively, you can ship the exported Realm using the `assets` then specify it in the `RealmConfiguration` to be sure you're using the correct file. `RealmConfiguration config = new RealmConfiguration.Builder() .assetFile("myExported.realm") .build();` – Nabil Hachicha Aug 02 '17 at 13:51
  • I did this using two ways: the first way - i found my Realm file in app package by Android Monitor and changed them; the second way I found my realm file, code: File realmFile = new File(getRealm().getPath()) and then wrote new file using outputStream and InputStream. In both variants I see a new realm file, but I can't use it. – Delphian Aug 02 '17 at 14:21
  • can you try the `assetFile` approach? – Nabil Hachicha Aug 03 '17 at 11:19
  • Nabil, I wrote above about this approach. File doesn't rewrite. I see the same file. – Delphian Aug 04 '17 at 08:41
  • I added above how I can import file but I can't see my Objects after that import. – Delphian Aug 04 '17 at 09:11

2 Answers2

1

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 the assets folder of your APK. Also specifying a name will create a new (empty) Realm.

Try the following configuration instead.

RealmConfiguration config = new RealmConfiguration.Builder().assetFile("myExported").build();

Your APK:

└── src └── main ├── AndroidManifest.xml ├── assets │   └── myExported.realm ├── java └── res

Nabil Hachicha
  • 216
  • 1
  • 4
  • I can't add this file to asset folder because this directory have only read permission and limit 10 MB. I download new Realm file from Firebase and place it into the DCIM or cache it doesn't matter. – Delphian Aug 04 '17 at 12:43
  • You need to ship the file `myExported.realm` inside the `assets` folder before you build your APK. Don't try to `adb push` the file after the APK is installed. – Nabil Hachicha Aug 04 '17 at 13:17
  • It makes no sense. My task is for users in real time to exchange realm databases, sending them to each other. – Delphian Aug 04 '17 at 13:51
0
.assetFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath())

should be

.directory(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM))
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428