In the apk folder of my application I have a database. I want to connect to that database. Cpodo.Cpodo
is the name of the apk folder. Doing the following:
string databasesPath = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/Android/data/Cpodo.Cpodo"; var connection = new SQLiteConnection(System.IO.Path.Combine(databasesPath, databaseName))
I am able to connect to the db
but is there a single command to get the apk folder full path so I don't have to type all that:
Android.OS.Environment.ExternalStorageDirectory.ToString() + "/Android/data/Cpodo.Cpodo"
Because it is working for my phone but may be in another phones the apk is located in different place and in that case this line of code won't work.
For /storage/emulated/0/Android/data/Cpodo.Cpodo/files, you can use:this.GetExternalFilesDir(null).AbsolutePath;
But please do notice that: as Android doc says here: if files are in a directory within external storage, other apps can get access to the files.
I'm wondering why /data/user/0/Cpodo.Cpodo/files this folder doesn't work for you, since it's working good on my side. Please let me know if you want to further look into this questions and provide some extra informations that may be related.
I can get to /data/user/0/packagename with:
this.DataDir.AbsolutePath
But didn't find programatical way to the external data directory.
As Android storage guideline declared, data/files that's specific for your app can be stored either in internal storage(/data/user/0/packagename/...) or external storage(/storage/emulated/0/Android/data/packagename/...), both files will be removed on app uninstall. The only difference is that whether other apps are able to get access to your file. For database files, I think it would be better if you store it in the internal storage.
For which folder is the best for storing database file, actually when I go through guides for Xamarin Forms/Android database, there's code using LocalApplicationData, that is /data/user/0/packagename/files/.config:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
as well as Personal folder /data/user/0/packagename/files:
Environment.GetFolderPath (Environment.SpecialFolder.Personal)
So I think files and files/.config will surely be ok to store database file. For other folders, as long as it's not for specific use, then whether it's appropriate for databse, it's up to you.
Hope it helps.
Answers
You can use
string dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
to access the db as illustrated here: https://docs.microsoft.com/en-us/xamarin/android/data-cloud/data-access/using-sqlite-orm#basic-data-access-sample
I generally put the database
speakers.db
directy inside my apk folder like thatCpodo.Cpodo/speakers.db
. But as the value of the variabledbPath
:runtime is
/data/user/0/Cpodo.Cpodo/files
I decided to put the database in that folderCpodo.Cpodo/files/speakers.db
. When I run the app I get the following exceptionno such table: speakers
. When I give the following value to thedatabasesPath
variable:I don't get exceptions, I am able to connect to the database. The value of
databasesPath
runtime is"/storage/emulated/0/Android/data/Cpodo.Cpodo/files"
For /storage/emulated/0/Android/data/Cpodo.Cpodo/files, you can use:
this.GetExternalFilesDir(null).AbsolutePath;
But please do notice that: as Android doc says here: if files are in a directory within external storage, other apps can get access to the files.
I'm wondering why /data/user/0/Cpodo.Cpodo/files this folder doesn't work for you, since it's working good on my side. Please let me know if you want to further look into this questions and provide some extra informations that may be related.
Originally I intended to put the database directly in
/storage/emulated/0/Android/data/Cpodo.Cpodo
folder. Can I get that path programatically and isfiles
folder the most appropriate place for databases or if they are directly in theapk
folder it's also OK?I can get to /data/user/0/packagename with:
this.DataDir.AbsolutePath
But didn't find programatical way to the external data directory.
As Android storage guideline declared, data/files that's specific for your app can be stored either in internal storage(/data/user/0/packagename/...) or external storage(/storage/emulated/0/Android/data/packagename/...), both files will be removed on app uninstall. The only difference is that whether other apps are able to get access to your file. For database files, I think it would be better if you store it in the internal storage.
For which folder is the best for storing database file, actually when I go through guides for Xamarin Forms/Android database, there's code using LocalApplicationData, that is /data/user/0/packagename/files/.config:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
as well as Personal folder /data/user/0/packagename/files:
Environment.GetFolderPath (Environment.SpecialFolder.Personal)
So I think files and files/.config will surely be ok to store database file. For other folders, as long as it's not for specific use, then whether it's appropriate for databse, it's up to you.
Hope it helps.
By the way I don't have any external storage on my phone. I don't know why it is showing that the
apk
is located in/storage/emulated/0/Android/data/Cpodo.Cpodo
.I don't connect to the
db
from anactivity
but from normal classDatabaseHelper
where I have the methodGetAllFromTable
(where I do the connection). In theDatabaseHelper
class I don't have access toGetExternalFilesDir(null).AbsolutePath
method. Can I get/storage/emulated/0/Android/data/Cpodo.Cpodo
path programatically in a normal class without passing to that class an instance of an activity. I don't want theDatabaseHelper
class to be coupled with activities etc. I just want to use it in an activity just to get a list of object(database records)?I didn't find something similar with GetExternalFilesDir(null).AbsolutePath when not using an activity, In this case, I think it would better for you to use the internal storage since it is dependent from Activity.