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

JavaAndroidRealm

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 - Java

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 - Java

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

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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.

Solution 9 - Java

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)

Solution 10 - Java

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.

Solution 11 - Java

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/

Solution 12 - Java

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.

Solution 13 - Java

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.

Solution 14 - Java

> 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

Solution 15 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndy JoyceView Question on Stackoverflow
Solution 1 - JavaChristian MelchiorView Answer on Stackoverflow
Solution 2 - JavaJemshit IskenderovView Answer on Stackoverflow
Solution 3 - JavaAppersideView Answer on Stackoverflow
Solution 4 - JavaruclipView Answer on Stackoverflow
Solution 5 - JavakrhiteshView Answer on Stackoverflow
Solution 6 - JavaAveekView Answer on Stackoverflow
Solution 7 - JavaRooneyView Answer on Stackoverflow
Solution 8 - JavaPedro Alvarez-TabioView Answer on Stackoverflow
Solution 9 - JavaBruguiView Answer on Stackoverflow
Solution 10 - JavakuzduView Answer on Stackoverflow
Solution 11 - JavaMehdi SakoutView Answer on Stackoverflow
Solution 12 - JavaIntiView Answer on Stackoverflow
Solution 13 - JavaKosoView Answer on Stackoverflow
Solution 14 - JavaRadin RethView Answer on Stackoverflow
Solution 15 - JavaHe Yifei 何一非View Answer on Stackoverflow