Home > Enterprise >  How to open SQLite DB created by one Xamarin Android app in another app?
How to open SQLite DB created by one Xamarin Android app in another app?

Time:05-18

I have a Xamarin Android app which creates SQLite DB. After reinstaling this app I am not able open the DB with a new version of the app. For the testing purpose the DB file is located at a Download folder. When the app is just updated not reinstaled it access the DB normaly.

A motivation: To get an independent DB file which could be used as a backup in case of moving app to another device.

EDIT:

App.xaml.cs:

public partial class App : Application
{
    private readonly static string _dirPath = "/storage/emulated/0/Download/InventoryApp";
    private static InvDB _database;            
    public static InvDB Database
    {
        get
        {
            if (_database == null)
            {
                string dbFileName = "InventoryDB.db3";
                _database = new InvDB(Path.Combine(_dirPath, dbFileName));
            }
            return _database;
        }

    }
    // ....
}

Database class constructor:

public class InvDB
{
    readonly SQLiteAsyncConnection conn;

    public const SQLite.SQLiteOpenFlags Flags =        
    SQLite.SQLiteOpenFlags.ReadWrite |
    SQLite.SQLiteOpenFlags.Create |
    SQLite.SQLiteOpenFlags.FullMutex;

    public InvDB(string dbPath)
    {
        if(File.Exists(dbPath))
        {
            conn = new SQLiteAsyncConnection(dbPath, Flags);
            conn.CreateTableAsync<Item>().Wait();
            conn.CreateTableAsync<Room>().Wait();
        }
    }
    // ....
}

CodePudding user response:

At first, the READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE permissions need the user to granted by himself, so you not only need to add them into the manifest file, but also need to do a permission request to get the permission.

You can check this link:Can't write to Android External storage even with permissions set

If it still doesn't work, you can try to grant the app Access All Files permission, but I don't recommend to do this.

You can check the answer I posted a few months ago:How to Request manage_external_storage Permission in Xamarin.Android

Update:

It seems that the Download folder is special, I create a txt file in it and try to write it with the write external storage permission. But I failed, the official document says:

If your app wants to access a file within the MediaStore.Downloads collection that your app didn't create, you must use the Storage Access Framework. To learn more about how to use this framework, see the guide on how to access documents and other files.

You can check the official document:https://developer.android.com/training/data-storage/shared/media#scoped_storage_enabled

And then I add the <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> into the manifest file and use the code in the link I provide above to request the MANAGE_EXTERNAL_STORAGE permission. I write the file successfully.

So you can also try to do this or use the Storage Access Framework just like the official document says to open the database file in the download folder.

  • Related