无论如何,我可以选择Realm数据库的路径,那么数据库将在那里,应用程序将使用它吗?
答案 0 :(得分:3)
Realm Configuration Builder允许配置folder path。因此,您应该能够将其指向适当的sdcard文件夹。示例:
File folder = new File("/data/user/0/...");
RealmConfiguration realmConfig = new RealmConfiguration.Builder(folder).build();
您还可以通过以下方式检查是否正确设置:
realmConfig.getRealmFolder().getAbsolutePath()
答案 1 :(得分:2)
Realm Configuration Builder现在有两种方法来定义数据库路径和文件名:
在您的应用程序类中使用的方法示例:
@Override
public void onCreate() {
super.onCreate();
initializeRealm();
}
private void initializeRealm() {
Realm.init(this);
RealmConfiguration.Builder realmBuilder = new RealmConfiguration.Builder();
realmBuilder.directory(new File(getRealmPath(this)));
realmBuilder.name("realmDataBase.realm");
RealmConfiguration config = realmBuilder.build();
Realm.setDefaultConfiguration(config);
}
public String getRealmPath(Context context) {
String externalStorageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String packageName = context.getApplicationContext().getPackageName();
return externalStorageDir + File.separator + "Android" + File.separator + "data" + File.separator + packageName + File.separator + "realm" + File.separator;
}