Forum Xamarin.Android

How to add SQLite database file into apk file when generating the apk file(Xamarin.Android)?

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

I want to add SQLite database inside an apk file when generating the apk file so that when I install the apk on the phone, the database to be inside the apk folder of the app. For example if Cpodo.Cpodo is the name of the apk folder, I want after installation the SQLite database file (speakers.db) to be inside the apk folder like that:

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We could embed a .db file in the project under Assets folder and access it through:

    File cacheFile = new File(CacheDir, "sample.db");
    try
    {
        var inputStream = Assets.Open("sample.db");
        try
        {
            FileOutputStream outputStream = new FileOutputStream(cacheFile);
            try
            {
                byte[] buf = new byte[1024];
                int len;
                while ((len = inputStream.Read(buf)) > 0)
                {
                    outputStream.Write(buf, 0, len);
                }
            }
            finally
            {
                outputStream.Close();
            }
        }
        finally
        {
            inputStream.Close();
        }
    }
    catch (IOException ex)
    {
    }
    
    var db = new SQLiteConnection(cacheFile.AbsolutePath);
    db.CreateTable<Model>();
    var data = db.Table<Model>().ToList();
    
  • AnonymousUserAnonymousUser USMember ✭✭✭
    edited May 2020

    So it is impossible to add a file into an apk file when generating it? If I want a file to appear in the phone when I install an apk file , I must put that file inside the Xamarin.Android project (in Assets folder for example), if it's yes, where do I look for that file in the phone after the instalation of the apk or after the deploy of the project?

  • LandLuLandLu Member, Xamarin Team Xamurai

    We need to start from the programmatical side. Once we put the file in the project, it will be compiled into .apk.
    We can't individually add an extra file when generating .apk file.

  • AnonymousUserAnonymousUser USMember ✭✭✭
    edited May 2020

    If we put sample.db inside the Assets folder of the project where do I look for sample.db in the phone after the installation of the apk file or after the deploy of the app. I looked inside the apk folder of the app but it wasn't there.

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could store it on the local storage after enabling the write external storage permissions.
    And then we can find it on your phone in the storage.
    May I ask why you want to embed this file in .apk?
    We definitely could achieve it through other approaches instead of the hacky one.

  • AnonymousUserAnonymousUser USMember ✭✭✭

    I wanted to embed the database file into the apk because I was asked to do it that way by person who ordered the project.

  • LandLuLandLu Member, Xamarin Team Xamurai

    The files in the project will all be packaged into .apk.
    I still can't understand why do you want to embed it directly into .apk. And we can't do it programmatically.
    We could manipulate the files in the application and store them in the external storage if you want.

Sign In or Register to comment.