How to move my Realm file in SD card?

。_饼干妹妹 提交于 2019-12-12 09:56:56

问题


How to move my Realm file in SD card?

And not just move, but also to connect to my application?


回答1:


Realm also has constructors for opening any file: http://realm.io/docs/java/0.80.0/api/io/realm/Realm.html#getInstance-java.io.File-java.lang.String-

So something like this should work:

Realm realm = Realm.getInstance(Environment.getExternalStorageDirectory());

And remember to add this to your manifest:

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

Also not all phones has a SD card and it might not be mounted so you will need additional checks for that if you don't want to encounter lots of weird crashes in production: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal



来源:https://stackoverflow.com/questions/29551664/how-to-move-my-realm-file-in-sd-card

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!