1

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();
        }
    }
    // ....
}
petr.f77
  • 29
  • 5
  • 1
    do you have all the necessary permissions? Depending on the Android version there are different mechanisms for accessing the public Downloads folder. You haven't provided any code illustrating what you're attempting to do so it's difficult to provide specific advice – Jason May 13 '22 at 21:01
  • @Jason I have a READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE permissions in a manifest file, I added some code to question – petr.f77 May 14 '22 at 05:33

1 Answers1

0

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.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • I am not sure if this explains why is the DB available by the first version of app, while it is not after reinstaling. – petr.f77 May 16 '22 at 07:41
  • Practice produces true knowledge, you can check the app whether have the permission when it reinstall with a run-time permission check. With my using of android, the permission need to be granted after reinstalling.@petr.f77 – Liyun Zhang - MSFT May 16 '22 at 07:46
  • Thanks. I added permission request, performed StoragePermission run-time check `var status = await CrossPermissions.Current.CheckPermissionStatusAsync();`, which is granted now, but there is still the same err: _Could not open database file: /storage/emulated/0/Download/InventoryApp/InventoryDB.db3 (CannotOpen))_ – petr.f77 May 16 '22 at 09:42
  • You can check the update answer and I will be looking forward to your reply. @petr.f77 – Liyun Zhang - MSFT May 17 '22 at 03:03
  • I tried add MANAGE_EXTERNAL_STORAGE but not works for me. I didn't try mentioned framework. Now I am going to use app folder for DB file and make backup another way which is probably standard solution.. – petr.f77 May 17 '22 at 14:46