Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 694
lcantoral Avatar asked Feb 06 '18 14:02

lcantoral


People also ask

How do I change the default location of a database?

In Object Explorer, right-click on your server and click Properties. In the left panel on that Properties page, click the Database settings tab. In Database default locations, view the current default locations for new data files and new log files.

How do I change the default location of the log file?

To change a default location, enter a new default pathname in the Data or Log field, or click the browse button to find and select a pathname.

Where are the default data and default log stored in SQL Server?

When you save the location, it's saved as DefaultData and DefaultLog under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer The Work around to resolve this issue is to restart the SQL server or SQL services 1.

How do I change the default home folder path in Windows?

Right-click the new DefaultHomeFolder key, select New > String Value, and name it Location. Double-click the new Location string value and change its value to the path of the desired Home folder. Is This Content Helpful?


1 Answers

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

UPDATE

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>
like image 57
joe06 Avatar answered Sep 16 '22 18:09

joe06