How do I view my Realm file in the Realm Browser?

104,198

Solution 1

Currently the Realm Browser doesn't support accessing databases directly on the device, so you need to copy the database from the emulator/phone to view it. That can be done by using ADB:

adb pull /data/data/<packagename>/files/ .

That command will pull all Realm files created using Realm.getInstance(new RealmConfiguration.Builder().build()) . The default database is called default.realm.

Note that this will only work on a emulator or if the device is rooted.

Solution 2

Now you can view Realm DB on Chrome browser using Stetho, developed by Facebook. By default, Stetho allows to view Sqlite, network, sharedpreferences but with additional plugin here allows to view Realm as well.

After configuring your Application class with above libraries, while app is running and connected, open Chrome browser and navigate chrome://inspect to see


enter image description here

Then Resources->Web Sql->default.realm


enter image description here

Solution 3

You can also pull your file from any NON-rooted device using the ADB shell and run-as command.

You can use these commands to pull from your app's private storage a database named your_database_file_name located in the files folder:

adb shell "run-as package.name chmod 666 /data/data/package.name/files/your_database_file_name"

// For devices running an android version lower than Android 5.0 (Lollipop)
adb pull /data/data/package.name/files/your_database_file_name

// For devices running an Android version equal or grater
// than Android 5.0 (Lollipop)
adb exec-out run-as package.name cat files/your_database_file_name > your_database_file_name
adb shell "run-as package.name chmod 600 /data/data/package.name/files/your_database_file_name"

Solution 4

If you are lazy to get the realm database file every time with adb, you could add an export function to your android code, which send you an email with the realm database file as attachment.

Here an example:

public void exportDatabase() {

    // init realm
    Realm realm = Realm.getInstance(getActivity());

    File exportRealmFile = null;
    try {
        // get or create an "export.realm" file
        exportRealmFile = new File(getActivity().getExternalCacheDir(), "export.realm");

        // if "export.realm" already exists, delete
        exportRealmFile.delete();

        // copy current realm to "export.realm"
        realm.writeCopyTo(exportRealmFile);

    } catch (IOException e) {
        e.printStackTrace();
    }
    realm.close();

    // init email intent and add export.realm as attachment
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("plain/text");
    intent.putExtra(Intent.EXTRA_EMAIL, "YOUR MAIL");
    intent.putExtra(Intent.EXTRA_SUBJECT, "YOUR SUBJECT");
    intent.putExtra(Intent.EXTRA_TEXT, "YOUR TEXT");
    Uri u = Uri.fromFile(exportRealmFile);
    intent.putExtra(Intent.EXTRA_STREAM, u);

    // start email intent
    startActivity(Intent.createChooser(intent, "YOUR CHOOSER TITLE"));
}

Don't forget to add this user permission to your Android Manifest file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Solution 5

For Android (No need to root your device)

To obtain a copy of any of Realm database on your device, go to Device File Explorer in Android Studio.

Navigate to /data/data/your.package.name/files/.

There you will find your *.realm files. Right click, then Save As. Make sure to synchronize before you save them.

Use Realm Browser or any of these to view *.realm files:

Enter image description here

Share:
104,198
Andy Joyce
Author by

Andy Joyce

Junior Android Developer

Updated on May 19, 2021

Comments

  • Andy Joyce
    Andy Joyce almost 3 years

    I've just discovered Realm and wanted to explore it in more detail so I decided to create sample application and having a mess around with it. So far so good.

    However, one thing I haven't been able to work out just yet is how to view my database in the Realm Browser. How can this be done?

  • bmunk
    bmunk about 9 years
    You can also log the exact path from your app with: Log.d("", "path: " + realm.getPath());
  • ymerdrengene
    ymerdrengene almost 9 years
    This is especially good if you use Pushbullet. Then you can push a message to your Chrome browser and it will download the file. The file can then be opened in Realm Browser
  • careful7j
    careful7j over 8 years
    Failed to resolve: com.uphyca:stetho_realm:0.8.0 May anyone help me with this? I am sorry, I am not a mac user, so it looks like this way is the only I can browse my database file.
  • Jemshit Iskenderov
    Jemshit Iskenderov over 8 years
    Did you add url https://github.com/uPhyca/stetho-realm/raw/master/maven-repo as shown here github.com/uPhyca/stetho-realm
  • careful7j
    careful7j over 8 years
    My fault, thank you very much! I have missed this. Now it works as it should!
  • Jemshit Iskenderov
    Jemshit Iskenderov over 8 years
    So Realm Browser for Mac is useless. Who uses emulator
  • AlexKost
    AlexKost over 7 years
    Does the browser updates it's content in real time? After I set it up it shows the same content each time whereas content changes. Even after app deleting-installing again I expected realm to be empty, but it still shows the same content. Any ideas?
  • Jemshit Iskenderov
    Jemshit Iskenderov about 7 years
    I watch the changes of table "A" by clicking some other table, and then clicking table "A" again @AlexKost
  • jmartinalonso
    jmartinalonso about 7 years
    For non-rooted devices this script works : gist.github.com/jk2K/a66ed67b252b880824b32dd4952d1b39
  • Mike6679
    Mike6679 about 7 years
    I have large realm db that I only read from. I see the name of it under Web SQL but there are no fields / tables....is it just taking a long time to load?
  • Jemshit Iskenderov
    Jemshit Iskenderov about 7 years
    @Mike6679 not sure, i tried with about 10 tables and maybe 50 rows at most. File a bug on github if you can't see
  • Mike6679
    Mike6679 about 7 years
    see this post if anyone has issue with seeing fields: github.com/uPhyca/stetho-realm/issues/45 .... I see my fields but no data....hmmm
  • randy
    randy about 7 years
    if you get error like file not found execute adb root command first before pulling realm file .
  • Mike Axle
    Mike Axle about 7 years
    This works after using adb root but no matter what I do, the realm file says it's encrypted when using realm browssr. Yes I read about corruption and such but nothing I try is working at all. Did something change in realm?
  • JoshuaTree
    JoshuaTree about 7 years
    Agreed. Since we can set the application to debuggable, we can pull the file with this without root.
  • pratham kesarkar
    pratham kesarkar almost 7 years
    Thanks. however it's doesn't work with real device.It only works with Emulator
  • Aveek
    Aveek almost 7 years
    @prathamkesarkar, that is why I have written emulator or rooted device because you cannot get access to these folders in a normal device.
  • EGHDK
    EGHDK over 6 years
    Crashing on Android 8.0
  • Gideon Sassoon
    Gideon Sassoon over 6 years
    Problem with this solution is that if you have a lot of columns it becomes compressed and there's no horizontal scroll
  • Jemshit Iskenderov
    Jemshit Iskenderov over 6 years
    @AkashBisariya i havent tested with latest realm version. but did you click Resources tab? or did you enabled stetho realm plugin in application class?
  • Erum
    Erum over 6 years
    i can open default.realm but i used this code Realm.init(this); RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().name("RealmProjectName") .build(); Realm realm = null; try { realm = Realm.getInstance(realmConfiguration); } finally { if (realm != null) { realm.close(); } }i can pull this file but failed to open in Realm Studio.Any idea.unable to find data stored in tables ?
  • Francisco Castro
    Francisco Castro over 6 years
    mac adb default path .../Library/Android/sdk/platform-tools
  • JEGADEESAN S
    JEGADEESAN S over 6 years
    is there any other way to pull database from non-debuggable apk?
  • JEGADEESAN S
    JEGADEESAN S over 6 years
    is there any other way to pull database from non-debuggable app and non-rooted mobile?
  • Apperside
    Apperside over 6 years
    if that would be possible, the entire android OS would be completely unsafe, There is a chance it could be possible only on rooted devices, but I never did it
  • SkorpEN
    SkorpEN about 6 years
    This might not work on Samsung devices due to their buggy run-as implementations.
  • Aman Verma
    Aman Verma over 5 years
    Make sure your app is set to debuggable false in order to view the data.
  • krhitesh
    krhitesh over 5 years
    @AmanVerma It's USB debugging in Developer Options. USB debugging must be enabled to use Device File Explorer
  • M.kazem Akhgary
    M.kazem Akhgary over 5 years
    it also works on non-rooted device if the application is debuggable
  • B.shruti
    B.shruti over 5 years
    I am testing on Emulator but still my cmd shows remote object '/data/data/<packagename>/files/' does not exist
  • B.shruti
    B.shruti over 5 years
    Linux user's can download realm studio using realm.io/products/realm-studio and open realm file from there
  • B.shruti
    B.shruti over 5 years
    Anyone who is facing remote object doesn't exist on linux please scroll down and check @Kumar Hitesh answer.
  • Ender
    Ender about 5 years
    So, we can use this to import our edited realm back to its place? adb exec-out run-as package.name cat files/your_database_file_name < your_database_file_name
  • Ender
    Ender about 5 years
    You can just use strings default.realm to view the file at its location in files/default.realm.
  • Maria
    Maria about 5 years
    This is the most useful answer, thank you! Does not show the path for macOS, but it is still easy to locate the file.
  • Chris
    Chris about 5 years
    a working fork is located at github.com/wickedev/stetho-realm. I just set it up and is working on latest realm. Here is from another discussion with updated init example: stackoverflow.com/a/51381971