0

I am updating an application that works on API 29/Android Q devices but not Android 11. I understand that there are new scoped storage updates and am already receiving permissions from the user for

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

My code looks something like this:

//the path returns /data/user/0/com.company.MyApp/files/MyAppDB.sqlite
            var Path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var asyncConn = new SQLiteAsyncConnection(Path);
            try
            {
                await asyncConn.CreateTableAsync<SampleObject>();
                return true;
            }
            catch (Exception ex)
            { 
                return false; 
            }

When executing CreateTableAsync I receive an object reference error. This is only happening with Android 11 emulators/devices.

This does not seem to be an issue with SQLite but an issue accessing the sqlite file, or writing to it at least.

  • 1
    https://developer.android.com/about/versions/11/privacy/storage#maintain-compatibility-android-10 – Jason Oct 13 '21 at 14:53
  • Thank you, I have looked at that doc several times and tried implementing some of it. Are you saying to unset both flags for DEFAULT_SCOPED_STORAGE and FORCE_ENABLE_SCOPED_STORAGE? Do you have a link on how to do that as I cannot find anything for Xamarin – Daniel Mamnev Oct 13 '21 at 16:33
  • have you tried `requestLegacyExternalStorage`? – Jason Oct 13 '21 at 16:34
  • Yes, it is set to true. Per the docs it doesn't apply to apps targeting Android 11. – Daniel Mamnev Oct 13 '21 at 16:53
  • Android 11 has access restrictions on internal storage, what is the version of target in your project ? If it is lower than Android 10 ,even you're trying to deploy on a Android 11 , it could be solved by enabling `requestLegacyExternalStorage` , but if the project is targeting Android 11, only the external storage is allowed to access, you can use `getExternalFilesDirs()` to get the path ,pls check https://developer.android.com/about/versions/11/privacy/storage#app-specific-external – ColeX Oct 14 '21 at 09:39
  • @ColeX-MSFT I am trying to target Android 11 and changed the path to getExternalFilesDirs. Here is my path: `/storage/emulated/0/Android/data/com.company.MyApp/files/MyAppDB.sqlite` and unfortunately still getting an object reference error when running CreateTableAsync. – Daniel Mamnev Oct 14 '21 at 13:51
  • Try to use `getExternalStoragePublicDirectory` , check https://stackoverflow.com/a/66962161/8187800. – ColeX Oct 19 '21 at 09:32

0 Answers0