How can I change the default location of a Room database?

我的未来我决定 提交于 2020-01-13 09:31:51

问题


I want to change the location of the Android Room Database. I know that the database is inside of the files system, and I need to get root permissions, but I do not want to root my phone.

The idea is change the database location to SD card, and can access it without root my phone


回答1:


Just put the location path in the name of the database.
I.e.:

AppDatabase db = Room.databaseBuilder(getApplicationContext(),
        AppDatabase.class, "database-name").build();

Put the router in database name.
I.e.:

 AppDatabase db = Room.databaseBuilder(getApplicationContext(),
        AppDatabase.class, "/storage/emulated/0/folder/database-name").build();

and do not forget to give the write permissions to the application

thx @vitidev



来源:https://stackoverflow.com/questions/48645801/how-can-i-change-the-default-location-of-a-room-database

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