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.
我刚刚发现了Realm,并且想要更详细地探索它,所以我决定创建示例应用程序并搞砸它。到现在为止还挺好。
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?
但是,我还没有解决的一件事是如何在Realm Browser中查看我的数据库。如何才能做到这一点?
12 个解决方案
#1
113
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:
目前,Realm Browser不支持直接在设备*问数据库,因此您需要从模拟器/手机中复制数据库以进行查看。这可以通过使用ADB来完成:
adb pull /data/data/<packagename>/files/ .
adb pull / data / data /
That command will pull all Realm files created using Realm.getInstance(new RealmConfiguration.Builder().build())
. The default database is called default.realm
.
该命令将获取使用Realm.getInstance(new RealmConfiguration.Builder()。build())创建的所有Realm文件。默认数据库名为default.realm。
Note that this will only work on a emulator or if the device is rooted.
请注意,这仅适用于模拟器或设备已植根。
#2
93
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.
现在,您可以使用Facebook开发的Stetho在Chrome浏览器上查看Realm DB。默认情况下,Stetho允许查看Sqlite,网络,共享偏好,但这里有额外的插件允许查看Realm。
After configuring your Application
class with above libraries, while app is running and connected, open Chrome browser and navigate chrome://inspect
to see
使用上述库配置Application类后,在应用程序运行并连接时,打开Chrome浏览器并导航chrome:// inspect以查看
Then Resources->Web Sql->default.realm
然后是Resources-> Web Sql-> default.realm
#3
51
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.
如果你每次使用adb都懒得获取领域数据库文件,你可以在你的android代码中添加一个导出函数,它会向你发送一封带有领域数据库文件作为附件的电子邮件。
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:
不要忘记将此用户权限添加到Android Manifest文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#4
47
You can also pull your file from any NON-rooted device using the ADB shell and run-as command.
您还可以使用ADB shell和run-as命令从任何非root用户设备中提取文件。
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:
您可以使用这些命令从应用程序的专用存储中提取位于files文件夹中名为your_database_file_name的数据库:
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"
#5
14
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.
有一个解决方法。您可以直接从设备监视器访问该文件。只有在使用模拟器或root设备时才能访问此目录。
In Android Studio:
在Android Studio中:
Select
Menu Tools → Android → Android Device Monitor → File Explorer → data → data → (Your Package Name) → files → *db.realm
菜单工具→Android→Android设备监控→文件浏览器→数据→数据→(您的包名称)→文件→* 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.
从Android Studio 3 canary 1开始,已经引入了Device File Explorer。你需要在这里查看领域文件。然后,(选择您的包裹)→选择领域文件→右键单击并保存。
And open the file into the Realm Browser. You can see your data now.
并将文件打开到Realm Browser中。您现在可以看到您的数据。
#6
13
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':
首先,您可以将位于'/ data / data / packagename / files'中的领域文件复制到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工具来拉这个文件:
$ adb pull /sdcard/default.realm .
$ adb pull /sdcard/default.realm。
Now you can open the file in the Realm Browser.
现在,您可以在Realm Browser中打开该文件。
#7
7
For Android (rooted and unrooted)
To obtain a copy of any of Realm database on your device, go to Device File Explorer in Android Studio.
要获取设备上任何Realm数据库的副本,请转到Android Studio中的设备文件资源管理器。
Navigate to /data/data/your.package.name/files/
.
导航到/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.
在那里你会找到你的* .realm文件。右键单击,然后单击另存为。确保在保存之前进行同步。
Use Realm Browser or any of these to view *.realm files:
使用Realm Browser或其中任何一个来查看* .realm文件:
#8
4
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命令,不需要将手机设置为root。只有前提条件是您必须在目标手机上安装应用程序的调试版本。
$ 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.
您将从本地目录中的任何手机获得数据库的副本,然后可以将其加载到领域浏览器中。
#9
1
Here is my ready-to-use shell script. Just change package name and your adb paths then the script will do the necessary.
这是我的即用型shell脚本。只需更改包名称和adb路径,脚本就可以执行必要的操作。
#!/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/
有关此博客文章的更多详细信息:http://medyo.github.io/2016/browse-populate-and-export-realm-database-on-android/
#10
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:
- 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 appendplatform-tools
to that path. - Use your app's fully qualified name for the
run-as
argument - Decide where you want to copy the realm file to
找到adb安装的路径。如果您正在使用Android Studio,请查看文件>项目结构> SDK位置> Android SDK位置,并将平台工具附加到该路径。
使用应用程序的完全限定名称作为run-as参数
确定要将realm文件复制到的位置
NB: The file is called default.realm because I haven't changed its name when configuring it - yours may be different.
注意:该文件名为default.realm,因为我在配置时没有更改其名称 - 您的名称可能不同。
#11
1
You have few options to view your android realm files:
您有几个选项来查看您的Android领域文件:
-
Like @Christian Melchior said you can pull your realm database from device and open it on your mac using OSX Realm Browser
就像@Christian Melchior说你可以从设备中提取你的领域数据库并使用OSX领域浏览器在你的mac上打开它
-
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.
您可以使用我创建的第三方Android Realm Browser,使用realm进行android开发更容易一些。应用程序将向您显示设备上的所有领域文件,您可以在测试应用程序时实时查看所有领域文件。
-
You can use Chrome browser Stetho Full description how to use Setho is provided by @Jemshit Iskendero answer.
你可以使用Chrome浏览器Stetho完整描述如何使用Setho是由@Jemshit Iskendero提供的答案。
#12
0
Here is a shell for lazy people like me :)
这是一个像我这样的懒人的shell :)
The .realm
file will be stored inside the tmpRealm/
folder next to the .sh
file.
.realm文件将存储在.sh文件旁边的tmpRealm /文件夹中。
#!/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:
或者如果你想让tmpRealm /在SD卡上:
#!/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
113
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:
目前,Realm Browser不支持直接在设备*问数据库,因此您需要从模拟器/手机中复制数据库以进行查看。这可以通过使用ADB来完成:
adb pull /data/data/<packagename>/files/ .
adb pull / data / data /
That command will pull all Realm files created using Realm.getInstance(new RealmConfiguration.Builder().build())
. The default database is called default.realm
.
该命令将获取使用Realm.getInstance(new RealmConfiguration.Builder()。build())创建的所有Realm文件。默认数据库名为default.realm。
Note that this will only work on a emulator or if the device is rooted.
请注意,这仅适用于模拟器或设备已植根。
#2
93
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.
现在,您可以使用Facebook开发的Stetho在Chrome浏览器上查看Realm DB。默认情况下,Stetho允许查看Sqlite,网络,共享偏好,但这里有额外的插件允许查看Realm。
After configuring your Application
class with above libraries, while app is running and connected, open Chrome browser and navigate chrome://inspect
to see
使用上述库配置Application类后,在应用程序运行并连接时,打开Chrome浏览器并导航chrome:// inspect以查看
Then Resources->Web Sql->default.realm
然后是Resources-> Web Sql-> default.realm
#3
51
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.
如果你每次使用adb都懒得获取领域数据库文件,你可以在你的android代码中添加一个导出函数,它会向你发送一封带有领域数据库文件作为附件的电子邮件。
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:
不要忘记将此用户权限添加到Android Manifest文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#4
47
You can also pull your file from any NON-rooted device using the ADB shell and run-as command.
您还可以使用ADB shell和run-as命令从任何非root用户设备中提取文件。
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:
您可以使用这些命令从应用程序的专用存储中提取位于files文件夹中名为your_database_file_name的数据库:
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"
#5
14
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.
有一个解决方法。您可以直接从设备监视器访问该文件。只有在使用模拟器或root设备时才能访问此目录。
In Android Studio:
在Android Studio中:
Select
Menu Tools → Android → Android Device Monitor → File Explorer → data → data → (Your Package Name) → files → *db.realm
菜单工具→Android→Android设备监控→文件浏览器→数据→数据→(您的包名称)→文件→* 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.
从Android Studio 3 canary 1开始,已经引入了Device File Explorer。你需要在这里查看领域文件。然后,(选择您的包裹)→选择领域文件→右键单击并保存。
And open the file into the Realm Browser. You can see your data now.
并将文件打开到Realm Browser中。您现在可以看到您的数据。
#6
13
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':
首先,您可以将位于'/ data / data / packagename / files'中的领域文件复制到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工具来拉这个文件:
$ adb pull /sdcard/default.realm .
$ adb pull /sdcard/default.realm。
Now you can open the file in the Realm Browser.
现在,您可以在Realm Browser中打开该文件。
#7
7
For Android (rooted and unrooted)
To obtain a copy of any of Realm database on your device, go to Device File Explorer in Android Studio.
要获取设备上任何Realm数据库的副本,请转到Android Studio中的设备文件资源管理器。
Navigate to /data/data/your.package.name/files/
.
导航到/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.
在那里你会找到你的* .realm文件。右键单击,然后单击另存为。确保在保存之前进行同步。
Use Realm Browser or any of these to view *.realm files:
使用Realm Browser或其中任何一个来查看* .realm文件:
#8
4
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命令,不需要将手机设置为root。只有前提条件是您必须在目标手机上安装应用程序的调试版本。
$ 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.
您将从本地目录中的任何手机获得数据库的副本,然后可以将其加载到领域浏览器中。
#9
1
Here is my ready-to-use shell script. Just change package name and your adb paths then the script will do the necessary.
这是我的即用型shell脚本。只需更改包名称和adb路径,脚本就可以执行必要的操作。
#!/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/
有关此博客文章的更多详细信息:http://medyo.github.io/2016/browse-populate-and-export-realm-database-on-android/
#10
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:
- 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 appendplatform-tools
to that path. - Use your app's fully qualified name for the
run-as
argument - Decide where you want to copy the realm file to
找到adb安装的路径。如果您正在使用Android Studio,请查看文件>项目结构> SDK位置> Android SDK位置,并将平台工具附加到该路径。
使用应用程序的完全限定名称作为run-as参数
确定要将realm文件复制到的位置
NB: The file is called default.realm because I haven't changed its name when configuring it - yours may be different.
注意:该文件名为default.realm,因为我在配置时没有更改其名称 - 您的名称可能不同。
#11
1
You have few options to view your android realm files:
您有几个选项来查看您的Android领域文件:
-
Like @Christian Melchior said you can pull your realm database from device and open it on your mac using OSX Realm Browser
就像@Christian Melchior说你可以从设备中提取你的领域数据库并使用OSX领域浏览器在你的mac上打开它
-
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.
您可以使用我创建的第三方Android Realm Browser,使用realm进行android开发更容易一些。应用程序将向您显示设备上的所有领域文件,您可以在测试应用程序时实时查看所有领域文件。
-
You can use Chrome browser Stetho Full description how to use Setho is provided by @Jemshit Iskendero answer.
你可以使用Chrome浏览器Stetho完整描述如何使用Setho是由@Jemshit Iskendero提供的答案。
#12
0
Here is a shell for lazy people like me :)
这是一个像我这样的懒人的shell :)
The .realm
file will be stored inside the tmpRealm/
folder next to the .sh
file.
.realm文件将存储在.sh文件旁边的tmpRealm /文件夹中。
#!/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:
或者如果你想让tmpRealm /在SD卡上:
#!/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: