如何更改会议室数据库的默认位置? [英] How can I change the default location of a Room database?
问题描述
我想更改Android Room数据库的位置.我知道该数据库位于文件系统内部,我需要获得root权限,但是我不想将我的手机设为root.
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.
这个想法是将数据库位置更改为SD卡,并且无需root即可访问它
The idea is change the database location to SD card, and can access it without root my phone
推荐答案
只需将位置路径放在数据库名称中即可.
即:
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();
,不要忘记为应用程序授予写权限
thx @vitidev
thx @vitidev
更新
如果您定位到Android 10或更高版本,请在应用的清单文件中将requestLegacyExternalStorage的值设置为true:
If you target Android 10 or higher, set the value of requestLegacyExternalStorage to true in your app's manifest file:
<manifest ... >
<!-- This attribute is "false" by default on apps targeting
Android 10 or higher. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
这篇关于如何更改会议室数据库的默认位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!