Forum Xamarin.Android

How to get apk folder full path programatically (Xamarin.Android) ?

AnonymousUserAnonymousUser USMember ✭✭✭
edited March 2020 in Xamarin.Android

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.

Best Answers

Answers

  • NicoleLuNicoleLu Member, Xamarin Team Xamurai

    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

  • AnonymousUserAnonymousUser USMember ✭✭✭

    I generally put the database speakers.db directy inside my apk folder like that Cpodo.Cpodo/speakers.db. But as the value of the variable dbPath:

    string dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    var connection = new SQLiteConnection(System.IO.Path.Combine(dbPath, "speakers.db"));
    

    runtime is /data/user/0/Cpodo.Cpodo/files I decided to put the database in that folder Cpodo.Cpodo/files/speakers.db. When I run the app I get the following exception no such table: speakers. When I give the following value to the databasesPath variable:

    string databasesPath = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/Android/data/Cpodo.Cpodo/files";
    var connection = new SQLiteConnection(System.IO.Path.Combine(databasesPath, "speakers.db"));
    

    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"

  • AnonymousUserAnonymousUser USMember ✭✭✭
    edited March 2020

    Originally I intended to put the database directly in /storage/emulated/0/Android/data/Cpodo.Cpodo folder. Can I get that path programatically and is files folder the most appropriate place for databases or if they are directly in the apk folder it's also OK?

  • AnonymousUserAnonymousUser USMember ✭✭✭
    edited March 2020

    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 an activity but from normal class DatabaseHelper where I have the method GetAllFromTable(where I do the connection). In the DatabaseHelper class I don't have access to GetExternalFilesDir(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 the DatabaseHelper 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)?

  • NicoleLuNicoleLu Member, Xamarin Team Xamurai

    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.

Sign In or Register to comment.