139

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy Joyce
  • 2,802
  • 3
  • 15
  • 24
  • WANT THE EASY WAY? Skip the path-logging, adb commands, and headache. @devsmash has the answer: https://stackoverflow.com/a/49705298/483520 – Nolan Amy Aug 28 '18 at 08:44

15 Answers15

135

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.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
  • 25
    You can also log the exact path from your app with: Log.d("", "path: " + realm.getPath()); – bmunk Feb 21 '15 at 20:15
  • 40
    So Realm Browser for Mac is useless. Who uses emulator – Jemshit Feb 12 '16 at 08:50
  • 10
    For non-rooted devices this script works : https://gist.github.com/jk2K/a66ed67b252b880824b32dd4952d1b39 – jmartinalonso Mar 22 '17 at 09:45
  • 3
    if you get error like file not found execute adb root command first before pulling realm file . – randy Apr 06 '17 at 18:09
  • 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? – Mike Axle Apr 21 '17 at 10:08
  • 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 ? – Erum Dec 05 '17 at 10:26
  • is there any other way to pull database from non-debuggable app and non-rooted mobile? – JEGADEESAN S Jan 22 '18 at 14:39
  • it also works on non-rooted device if the application is debuggable – M.kazem Akhgary Oct 15 '18 at 05:44
  • I am testing on Emulator but still my cmd shows remote object '/data/data//files/' does not exist – B.shruti Dec 20 '18 at 11:53
  • Anyone who is facing remote object doesn't exist on linux please scroll down and check @Kumar Hitesh answer. – B.shruti Jan 03 '19 at 11:56
  • You can just use `strings default.realm` to view the file at its location in files/default.realm. – Ender Feb 22 '19 at 04:05
105

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

Jemshit
  • 9,501
  • 5
  • 69
  • 106
  • 2
    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. – careful7j Jan 15 '16 at 10:38
  • 5
    Did you add url `https://github.com/uPhyca/stetho-realm/raw/master/maven-repo` as shown here https://github.com/uPhyca/stetho-realm – Jemshit Jan 15 '16 at 14:49
  • 2
    My fault, thank you very much! I have missed this. Now it works as it should! – careful7j Jan 15 '16 at 15:05
  • 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? – AlexKost Feb 02 '17 at 23:47
  • 1
    I watch the changes of table "A" by clicking some other table, and then clicking table "A" again @AlexKost – Jemshit Feb 24 '17 at 06:28
  • 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? – Mike6679 Mar 28 '17 at 13:31
  • @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 – Jemshit Mar 28 '17 at 13:33
  • 1
    see this post if anyone has issue with seeing fields: https://github.com/uPhyca/stetho-realm/issues/45 .... I see my fields but no data....hmmm – Mike6679 Mar 28 '17 at 20:19
  • Problem with this solution is that if you have a lot of columns it becomes compressed and there's no horizontal scroll – Gideon Sassoon Aug 26 '17 at 12:46
  • @AkashBisariya i havent tested with latest realm version. but did you click Resources tab? or did you enabled stetho realm plugin in application class? – Jemshit Nov 28 '17 at 13:25
  • 1
    a working fork is located at https://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: https://stackoverflow.com/a/51381971 – Chris Apr 24 '19 at 04:43
58

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Apperside
  • 3,542
  • 2
  • 38
  • 65
  • 3
    Agreed. Since we can set the application to debuggable, we can pull the file with this without root. – JoshuaTree Apr 27 '17 at 12:53
  • is there any other way to pull database from non-debuggable apk? – JEGADEESAN S Jan 22 '18 at 14:37
  • 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 – Apperside Jan 22 '18 at 21:07
  • This might not work on Samsung devices due to their buggy run-as implementations. – SkorpEN Mar 21 '18 at 15:56
  • 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 Feb 22 '19 at 04:04
53

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" />
ruclip
  • 758
  • 10
  • 20
  • 3
    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 – ymerdrengene Aug 10 '15 at 08:49
  • 1
    Crashing on Android 8.0 – EGHDK Aug 23 '17 at 18:59
50

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

Community
  • 1
  • 1
krhitesh
  • 777
  • 9
  • 14
20

There is a workaround. You can directly access the file from the device monitor. You can access this directory only when you are using an emulator or rooted device.

In Android Studio:

Select

Menu ToolsAndroidAndroid Device MonitorFile Explorerdatadata → (Your Package Name) → files → *db.realm

Pull this file from the device:

Enter image description here

From Android Studio 3 canary 1, Device File Explorer has been introduced. You need to look the realm file here. Then, (select your package) → select the realm file → Right click and save.

Enter image description here

And open the file into the Realm Browser. You can see your data now.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aveek
  • 849
  • 1
  • 12
  • 28
14

You can access the realm file directly. Here is solution that I've used.

First you can copy the realm file that is located in '/data/data/packagename/files' to Environment.getExternalStorageDirectory()+'/FileName.realm':

public class FileUtil {
    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}

Realm realm = null;
try {
    realm = Realm.getInstance(this);
        File f = new File(realm.getPath());
        if (f.exists()) {
            try {
                FileUtil.copy(f, new File(Environment.getExternalStorageDirectory()+"/default.realm"));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
}
finally {
    if (realm != null)
        realm.close();
}

Second, use the ADB tool to pull that file like this:

$ adb pull /sdcard/default.realm .

Now you can open the file in the Realm Browser.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rooney
  • 1,195
  • 10
  • 18
7

Here's a solution that doesn't require your phone to be rooted, by making use of the run-as command present inside adb's shell. Only pre-condition is that you must have a debug build of your app installed on the target phone.

$ adb shell
$ run-as com.yourcompany.yourapp # pwd will return /data/data/com.yourcompany.yourapp
$ cp files/default.realm /sdcard
$ exit
$ exit
$ adb pull /sdcard/default.realm ~/Desktop # or wherever you want to put it

You'll have a copy of the DB from any phone inside your local directory, which you can then load onto the Realm Browser.

5

For over two years now, the Realm Browser has been available for every operating system (mac, linux, windows).

https://docs.realm.io/sync/realm-studio

Works straight forward.

kuzdu
  • 7,124
  • 1
  • 51
  • 69
4

You can now access it directly if you're using an emulator.

First Log the path where the file is in the emulator as @bmunk says:

Log.d(TAG, "path: " + realm.getPath());

Second Search it and do right click on the file and choose "Save As", on the dialog will appear the route where the file really is in your system.

enter image description here You can copy the route from the Save As dialog

And then, just paste the route on the "Open Local File" dialog of Realm Studio.

(I've tested this in Windows only)

Brugui
  • 574
  • 6
  • 20
  • 1
    This is the most useful answer, thank you! Does not show the path for macOS, but it is still easy to locate the file. – Maria Mar 10 '19 at 10:03
1

Here is my ready-to-use shell script. Just change package name and your adb paths then the script will do the necessary.

#!/bin/sh
ADB_PATH="/Users/medyo/Library/Android/sdk/platform-tools"
PACKAGE_NAME="com.mobiacube.elbotola.debug"
DB_NAME="default.realm"
DESTINATION_PATH="/Users/Medyo/Desktop/"
NOT_PRESENT="List of devices attached"
ADB_FOUND=`${ADB_PATH}/adb devices | tail -2 | head -1 | cut -f 1 | sed 's/ *$//g'`
if [[ ${ADB_FOUND} == ${NOT_PRESENT} ]]; then
    echo "Make sure a device is connected"
else
    ${ADB_PATH}/adb shell "
        run-as ${PACKAGE_NAME} cp /data/data/${PACKAGE_NAME}/files/${DB_NAME} /sdcard/
        exit
    "
    ${ADB_PATH}/adb pull "/sdcard/${DB_NAME}" "${DESTINATION_PATH}"
    echo "Database exported to ${DESTINATION_PATH}${DB_NAME}"
fi

More details on this blog post : http://medyo.github.io/2016/browse-populate-and-export-realm-database-on-android/

Mehdi Sakout
  • 773
  • 5
  • 8
1

Keeping it simple:

/Users/inti/Library/Android/sdk/platform-tools/adb exec-out run-as com.mydomain.myapp cat files/default.realm > ~/Downloads/default.realm

Explanation:

  1. Find the path to your adb install. If you're using Android Studio then look at File > Project Structure > SDK Location > Android SDK Location and append platform-tools to that path.
  2. Use your app's fully qualified name for the run-as argument
  3. Decide where you want to copy the realm file to

NB: The file is called default.realm because I haven't changed its name when configuring it - yours may be different.

Inti
  • 3,443
  • 1
  • 29
  • 34
1

You have few options to view your android realm files:

  1. Like @Christian Melchior said you can pull your realm database from device and open it on your mac using OSX Realm Browser

  2. You can use third party Android Realm Browser I created, to make android development with realm little bit easier. App will show you all realm files on your device, and you can view all your realm files real time while testing your app.

  3. You can use Chrome browser Stetho Full description how to use Setho is provided by @Jemshit Iskendero answer.

Koso
  • 3,200
  • 2
  • 20
  • 22
1

Realm Browser is Deprecated, Use Realm Studio instead.

HERE

View filepath

console.log(realm.path)

Login adb as root

adb root

Pull realm file to local dir

adb pull /data/data/{app.identifier.com}/files/default.realm .

Result view in Realm Studio

Result view in Realm studio

Radin Reth
  • 847
  • 8
  • 7
0

Here is a shell for lazy people like me :)

The .realm file will be stored inside the tmpRealm/ folder next to the .sh file.

#!/bin/sh
adb shell 'su -c "
cd /data/data/<packagename>/files
ls
rm -rf /data/local/tmp/tmpRealm/
mkdir /data/local/tmp/tmpRealm/
cp /data/data/com.arefly.sleep/files/* /data/local/tmp/tmpRealm
chown shell.shell /data/local/tmp/tmpRealm/*
"'
rm -rf ./tmpRealm/
adb pull /data/local/tmp/tmpRealm ./

Or if you prefer to let tmpRealm/ be on the SD card:

#!/bin/sh
adb shell 'su -c "
cd /data/data/com.arefly.sleep/files
ls
mount -o rw,remount $EXTERNAL_STORAGE/
rm -rf $EXTERNAL_STORAGE/tmpRealm
mkdir $EXTERNAL_STORAGE/tmpRealm
cp /data/data/com.arefly.sleep/files/* $EXTERNAL_STORAGE/tmpRealm
"'
rm -rf ./tmpRealm/
# http://unix.stackexchange.com/a/225750/176808
adb pull "$(adb shell 'echo "$EXTERNAL_STORAGE"' | tr -d '\r')/tmpRealm" ./

Reference:

  1. https://stackoverflow.com/a/28486297/2603230
  2. https://android.stackexchange.com/a/129665/179720
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
He Yifei 何一非
  • 2,592
  • 4
  • 38
  • 69