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?
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.
Library designed for viewing and editing Realm database files on Android devices. Main Functions. Additionally.
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.
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
Then Resources->Web Sql->default.realm
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"
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" />
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:
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 Tools → Android → Android Device Monitor → File Explorer → data → data → (Your Package Name) → files → *db.realm
Pull this file from the device:
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.
And open the file into the Realm Browser. You can see your data now.
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.
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:
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.run-as
argumentNB: The file is called default.realm because I haven't changed its name when configuring it - yours may be different.
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/
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.
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.
And then, just paste the route on the "Open Local File" dialog of Realm Studio.
(I've tested this in Windows only)
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.
You have few options to view your android realm files:
Like @Christian Melchior said you can pull your realm database from device and open it on your mac using OSX Realm Browser
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.
You can use Chrome browser Stetho Full description how to use Setho is provided by @Jemshit Iskendero answer.
Realm Browser is Deprecated, Use Realm Studio instead.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With