Java 如何在 Realm 浏览器中查看我的 Realm 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28478987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:18:33  来源:igfitidea点击:

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

javaandroidrealm

提问by Andy Joyce

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?

但是,我还无法解决的一件事是如何在领域浏览器中查看我的数据库。如何才能做到这一点?

采纳答案by Christian Melchior

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 浏览器不支持直接在设备上访问数据库,因此您需要从模拟器/手机复制数据库才能查看。这可以通过使用 ADB 来完成:

adb pull /data/data/<packagename>/files/ .

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.

该命令将拉取所有使用Realm.getInstance(new RealmConfiguration.Builder().build()). 默认数据库称为default.realm.

Note that this will only work on a emulator or if the device is rooted.

请注意,这仅适用于模拟器或设备已植根。

回答by ruclip

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 清单文件中:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

回答by Rooney

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 中打开该文件。

回答by Jemshit Iskenderov

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 hereallows to view Realm as well.

现在您可以使用Facebook 开发的Stetho在 Chrome 浏览器上查看 Realm DB 。默认情况下,Stetho 允许查看 Sqlite、网络、共享首选项,但通过附加插件也可以查看 Realm。

After configuring your Applicationclass with above libraries, while app is running and connected, open Chrome browser and navigate chrome://inspectto see

Application使用上述库配置您的类后,在应用程序运行和连接时,打开 Chrome 浏览器并导航chrome://inspect以查看



enter image description here

在此处输入图片说明

Then Resources->Web Sql->default.realm

然后 Resources->Web Sql->default.realm



enter image description here

在此处输入图片说明

回答by Apperside

You can also pull your file from any NON-rooted device using the ADB shell and run-as command.

您还可以使用 ADB shell 和 run-as 命令从任何非根设备中提取文件。

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"

回答by u2603230

Here is a shell for lazy people like me :)

这是一个像我这样的懒人的外壳:)

The .realmfile will be stored inside the tmpRealm/folder next to the .shfile.

.realm文件将存储在tmpRealm/文件旁边的.sh文件夹中。

#!/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. https://stackoverflow.com/a/28486297/2603230
  2. https://android.stackexchange.com/a/129665/179720
  1. https://stackoverflow.com/a/28486297/2603230
  2. https://android.stackexchange.com/a/129665/179720

回答by Mehdi

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/

回答by Aveek

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 ToolsAndroidAndroid Device MonitorFile Explorerdatadata→ (Your Package Name) → files→ *db.realm

菜单工具AndroidAndroid 设备监视器文件资源管理器数据数据→(您的包名称)→文件→ *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.

从 Android Studio 3 canary 1 开始,引入了设备文件资源管理器。您需要在此处查看领域文件。然后,(选择您的包)→ 选择领域文件 → 右键单击​​并保存。

Enter image description here

在此处输入图片说明

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

并在 Realm Browser 中打开文件。您现在可以查看您的数据。

回答by Inti

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 adbinstall. If you're using Android Studio then look at File > Project Structure > SDK Location > Android SDK Location and append platform-toolsto that path.
  2. Use your app's fully qualified name for the run-asargument
  3. Decide where you want to copy the realm file to
  1. 找到adb安装路径。如果您使用的是 Android Studio,请查看 File > Project Structure > SDK Location > Android SDK Location 并附platform-tools加到该路径。
  2. 使用您的应用程序的完全限定名称作为run-as参数
  3. 确定要将领域文件复制到的位置

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

注意:该文件名为 default.realm,因为我在配置它时没有更改它的名称 - 您的名称可能不同。

回答by Pedro Alvarez-Tabio

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

这是一个不需要手机root的解决方案,通过使用shell中的run-as命令adb。唯一的先决条件是您必须在目标手机上安装应用程序的调试版本。

$ 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.

您将在本地目录中的任何手机上拥有一份数据库副本,然后您可以将其加载到领域浏览器中。