Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 948
Andy Joyce Avatar asked Feb 12 '15 13:02

Andy Joyce


People also ask

Where are Realm files stored?

A new finder window will open, where you find your Realm inside in the following path (e.g.): AppData/Documents/default. realm (The directory '/private/var/mobile' is the path, which is used by iOS on the device filesystem.

What is Realm browser?

Library designed for viewing and editing Realm database files on Android devices. Main Functions. Additionally.


14 Answers

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.

like image 101
Christian Melchior Avatar answered Sep 30 '22 20:09

Christian Melchior


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

like image 44
Jemshit Iskenderov Avatar answered Sep 30 '22 20:09

Jemshit Iskenderov


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"
like image 30
Apperside Avatar answered Sep 30 '22 20:09

Apperside


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" />
like image 39
ruclip Avatar answered Oct 01 '22 20:10

ruclip


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

like image 28
krhitesh Avatar answered Oct 04 '22 20:10

krhitesh


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.

like image 44
Aveek Avatar answered Sep 30 '22 20:09

Aveek


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.

like image 38
Rooney Avatar answered Oct 03 '22 20:10

Rooney


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.

like image 34
Inti Avatar answered Sep 30 '22 20:09

Inti


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/

like image 21
Mehdi Sakout Avatar answered Oct 03 '22 20:10

Mehdi Sakout


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.

like image 27
Pedro Alvarez-Tabio Avatar answered Sep 30 '22 20:09

Pedro Alvarez-Tabio


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)

like image 35
Brugui Avatar answered Oct 04 '22 20:10

Brugui


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.

like image 26
kuzdu Avatar answered Oct 04 '22 20:10

kuzdu


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.

like image 24
Koso Avatar answered Sep 30 '22 20:09

Koso


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

like image 39
Radin Reth Avatar answered Oct 03 '22 20:10

Radin Reth