dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 528 forks source link

Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.' #3819

Closed stefanolson closed 5 years ago

stefanolson commented 5 years ago

Hi,

I'm not really sure if this is an entity framework issue or a Xamarin issue. But the same code, same dll (.net standard) works perfectly fine on Windows desktop so I'm assuming is related to Xamarin. This code worked fine the last time I built it for Xamarin a few months ago.

Steps to Reproduce

When I run my application and call this line: Database.EnsureCreated();

I get this exception: Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'

I have tried various different file name options, but this is the last I've tested: optionsBuilder.UseSqlite($"Data Source=test.db");

I've tried to remove EF core from all the libraries involved. Started out using EF core 2.26 (which I need to go back to), but tested EF core 3.0. Same problem. And upgraded to all the latest Xamarin nuget packages.

If I look back in the debug window I see this: 10-18 23:17:50.959 D/Mono (22379): Assembly Ref addref Microsoft.EntityFrameworkCore.Relational[0x712b9db700] -> System.Diagnostics.DiagnosticSource[0x713b7cc700]: 3 10-18 23:17:50.963 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.EntityFrameworkCore.Relational.resources.dll'. 10-18 23:17:50.963 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.EntityFrameworkCore.Relational.resources.dll'. 10-18 23:17:50.963 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.EntityFrameworkCore.Relational.resources.exe'. 10-18 23:17:50.963 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.EntityFrameworkCore.Relational.resources.exe'. 10-18 23:17:50.963 D/Mono (22379): Assembly Loader probing location: '/storage/emulated/0/Android/data/com.companyname.FmXamarin/files/.override/en-NZ/Microsoft.EntityFrameworkCore.Relational.resources.dll'. 10-18 23:17:51.321 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.EntityFrameworkCore.Relational.resources.dll'. 10-18 23:17:51.322 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.EntityFrameworkCore.Relational.resources.dll'. 10-18 23:17:51.322 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.EntityFrameworkCore.Relational.resources.exe'. 10-18 23:17:51.323 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.EntityFrameworkCore.Relational.resources.exe'. 10-18 23:17:51.323 D/Mono (22379): Assembly Loader probing location: '/storage/emulated/0/Android/data/com.companyname.FmXamarin/files/.override/en/Microsoft.EntityFrameworkCore.Relational.resources.dll'. 10-18 23:17:51.476 D/Mono (22379): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so'). 10-18 23:17:51.476 D/Mono (22379): Searching for 'sqlite3_open_v2'. 10-18 23:17:51.480 D/Mono (22379): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so'). 10-18 23:17:51.480 D/Mono (22379): Searching for 'sqlite3_errcode'. 10-18 23:17:51.482 D/Mono (22379): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so'). 10-18 23:17:51.482 D/Mono (22379): Searching for 'sqlite3_errmsg'. 10-18 23:17:51.484 D/Mono (22379): DllImport searching in: 'e_sqlite3' ('libe_sqlite3.so'). 10-18 23:17:51.485 D/Mono (22379): Searching for 'sqlite3_extended_errcode'. 10-18 23:17:51.488 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.Data.Sqlite.resources.dll'. 10-18 23:17:51.488 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.Data.Sqlite.resources.dll'. 10-18 23:17:51.488 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.Data.Sqlite.resources.exe'. 10-18 23:17:51.488 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.Data.Sqlite.resources.exe'. 10-18 23:17:51.489 D/Mono (22379): Assembly Loader probing location: '/storage/emulated/0/Android/data/com.companyname.FmXamarin/files/.override/en-NZ/Microsoft.Data.Sqlite.resources.dll'. 10-18 23:17:51.694 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.Data.Sqlite.resources.dll'. 10-18 23:17:51.694 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.Data.Sqlite.resources.dll'. 10-18 23:17:51.694 D/Mono (22379): Assembly Loader probing location: '/Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/sdks/out/android-arm64-v8a-release/lib/Microsoft.Data.Sqlite.resources.exe'. 10-18 23:17:51.695 D/Mono (22379): Assembly Loader probing location: '//Facades/Microsoft.Data.Sqlite.resources.exe'. 10-18 23:17:51.695 D/Mono (22379): Assembly Loader probing location: '/storage/emulated/0/Android/data/com.companyname.FmXamarin/files/.override/en/Microsoft.Data.Sqlite.resources.dll'. Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 14: 'unable to open database file'.'

Expected Behavior

Should open the database file, like it used to

Actual Behavior

Crashes.

Version Information

Microsoft Visual Studio Community 2019 Preview Version 16.4.0 Preview 2.0 VisualStudio.16.Preview/16.4.0-pre.2.0+29411.138 Microsoft .NET Framework Version 4.8.03752

Installed Version: Community

Visual C++ 2019 00435-60000-00000-AA532 Microsoft Visual C++ 2019

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASP.NET and Web Tools 2019 16.4.284.46185 ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019 16.4.284.46185 For additional information, visit https://www.asp.net/

AssemblyInfo Wizard 1.0 AssemblyInfo Wizard

Azure App Service Tools v3.0.0 16.4.284.46185 Azure App Service Tools v3.0.0

Azure Data Lake Node 1.0 This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio 2.4.1000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 16.4.284.46185 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.4.1000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools 3.4.0-beta2-19504-06+154af84a603094b52bd08b3366c4448f7481af52 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.0 (d16-2@8b56e20) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents 1.0 Fabric Diagnostic Events

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure HDInsight Azure Node 2.4.1000.0 HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service 2.4.1000.0 Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio 16.0 Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service 2.4.1000.0 Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node 1.0 Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 0x10 - v2.9.20905.5

Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.0.87+gbb515bf382 Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 16.5.21 (2604ba1) Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.4.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Office Developer Tools for Visual Studio 16.0.29319.00 Microsoft Office Developer Tools for Visual Studio

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.61910.04130 Microsoft SQL Server Data Tools

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

TypeScript Tools 16.0.10926.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.4.0-beta2-19504-06+154af84a603094b52bd08b3366c4448f7481af52 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.4 for F# 4.6 16.4.0-beta.19504.1+e3676d75d501a2df6789ad0f5ebc310bc2cee6b5 Microsoft Visual F# Tools 10.4 for F# 4.6

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions (Preview) 1.0 View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers

Visual Studio Tools for Kubernetes 1.0 Visual Studio Tools for Kubernetes

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Foo 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin 16.4.000.269 (d16-4@9202c30) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.4.0.289 (remotes/origin/d16-4@e3c99110c) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.4.15 (51cdaa7) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 10.1.0.1 (d16-4/ee1dfb6) Xamarin.Android Reference Assemblies and MSBuild support. Mono: 5281037 Java.Interop: xamarin/java.interop/d16-4@35b08ba ProGuard: xamarin/proguard/master@905836d SQLite: xamarin/sqlite/3.28.0@46204c4 Xamarin.Android Tools: xamarin/xamarin-android-tools/master@9f4ed4b

Xamarin.iOS and Xamarin.Mac SDK 13.8.1.1 (2e360ee) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Log File

--------- beginning of crash 10-18 06:44:55.643 19876 19876 E AndroidRuntime: FATAL EXCEPTION: main 10-18 06:44:55.643 19876 19876 E AndroidRuntime: Process: kodo.app.mymaccasreview, PID: 19876 10-18 06:44:55.643 19876 19876 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver com.onesignal.BootUpReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=kodo.app.mymaccasreview/com.onesignal.NotificationRestoreService (has extras) }: app is in background uid UidRecord{14e4da5 u0a290 RCVR idle change:uncached procs:1 proclist:19876, seq(0,0,0)} 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3590) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ActivityThread.access$1300(ActivityThread.java:235) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1779) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.os.Looper.loop(Looper.java:214) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6986) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=kodo.app.mymaccasreview/com.onesignal.NotificationRestoreService (has extras) }: app is in background uid UidRecord{14e4da5 u0a290 RCVR idle change:uncached procs:1 proclist:19876, seq(0,0,0)} 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1611) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:677) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:677) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.support.v4.content.WakefulBroadcastReceiver.startWakefulService(WakefulBroadcastReceiver.java:99) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at com.onesignal.BootUpReceiver.onReceive(BootUpReceiver.java:42) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3581) 10-18 06:44:55.643 19876 19876 E AndroidRuntime: ... 8 more 10-18 21:59:28.072 26920 26920 E AndroidRuntime: FATAL EXCEPTION: main 10-18 21:59:28.072 26920 26920 E AndroidRuntime: Process: com.companyname.FmXamarin, PID: 26920 10-18 21:59:28.072 26920 26920 E AndroidRuntime: android.runtime.JavaProxyThrowable: Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 14: 'unable to open database file'. 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC (System.Int32 rc, SQLitePCL.sqlite3 db) [0x00067] in :0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.Data.Sqlite.SqliteConnection.Open () [0x00109] in :0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection (System.Boolean errorsExpected) [0x00061] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open (System.Boolean errorsExpected) [0x0002a] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.Open (System.Boolean errorsExpected) [0x00000] in <99a85b9fe9c04315a16e823dd2cd2131>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Create () [0x0000b] in <99a85b9fe9c04315a16e823dd2cd2131>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated () [0x00010] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated () [0x00006] in <6bcdfaaeeef5425b99b4bfde66161a46>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at FmClient.Api.ServerConnections.Open () [0x00007] in <45c046c962ec418e8d23f05c01265c92>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at FmClient.ViewModel.MainWindowViewModel.Load () [0x00042] in <45c046c962ec418e8d23f05c01265c92>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at FmXamarin.MainPage.OnAppearing () [0x000b9] in <7eb15473b27042c7a8c98e555542d7d1>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b7_0 (System.Object state) [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Android.App.SyncContext+<>c__DisplayClass2_0.b0 () [0x00000] in <003e99f3940e4d98af4ba1b3fef4ec58>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <003e99f3940e4d98af4ba1b3fef4ec58>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <003e99f3940e4d98af4ba1b3fef4ec58>:0 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.53(intptr,intptr) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at mono.java.lang.RunnableImplementor.n_run(Native Method) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at android.os.Looper.loop(Looper.java:214) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6986) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 10-18 21:59:28.072 26920 26920 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

jonpryor commented 5 years ago

The problem is that the current working directory on Android is the root directory on the device, and you don't have permission to write there. Please provide a full path name in your Open invocation:

optionsBuilder.UseSqlite($"Data Source=/full/path/to/test.db");

Please see: https://www.jon-douglas.com/2016/12/28/xamarin-android-entity-framework/

For example, /full/path/to could be: System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal).

stefanolson commented 5 years ago

Like I said I tried lots of different folder options. This code worked three months previously and nothing changed around the path. The code that I was originally using was (this code shared with a desktop PC): var sqlitePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"OlsonSoftware\FinanceManager\"); Directory.CreateDirectory(sqlitePath); optionsBuilder.UseSqlite($"Data Source={sqlitePath}\fmd.db");

Also tried Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData

So presumably that should cover that path issue?

Stefan

campelo commented 4 years ago

I have created the file and works fine.

var sqlitePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"OlsonSoftware\FinanceManager"); Directory.CreateDirectory(sqlitePath); var fileName = $"{sqlitePath}\fmd.db"; if (!File.Exists(fileName)) File.Create(fileName); optionsBuilder.UseSqlite($"Data Source={fileName}");

stefanolson commented 4 years ago

Exactly - the the file path is not the problem I don't believe. The same code worked fine a few months ago. Nothing's changed. There must be an issue I'm trying to figure out how to track it down.

sk1llsh0t commented 4 years ago

Any luck getting this working? i've run into the exact same issue where it can't find the file even though file.exists says it is there.

sk1llsh0t commented 4 years ago

I have found that if i target android 10 (api level 29), it gives this error. I have to target android 9 (api level 28) for this to work correctly.

Are there any plans on making this android 10 compatible?

stefanolson commented 4 years ago

@sk1llsh0t I don't think I was targeting android 10 and I could never get it to work. I just gave up on using sqllite and used a xml file for my datastorage. The weird thing was prior to upgrading to a newer version of Xamarin it was working absolutely perfectly - my code didn't change.

sk1llsh0t commented 4 years ago

the strange thing for me is if i target android 10 and run it on a device <=android 9, it works. just on android 10 it is broken. so i'm stuck targeting android 9 until this is fixed.

I may have to consider using another database like realm. @stefanolson not sure i could go with an xml file and lose all the functionality of a true database engine. but i'm glad you found a solution that works for you.

sk1llsh0t commented 4 years ago

It looks like my issue has to do with permission changes made to sdcard storage in android 10. yippee for permission changes!

Kilowhisky commented 4 years ago

@sk1llsh0t do you know what new permissions you had to apply? This document indicated external storage but nothing about internal storage locations. https://developer.android.com/distribute/best-practices/develop/target-sdk

I am also utilizing the personal store that creates a file at.

/data/user/0/<my app id>/files/<my db>.db

This directory is no longer accessible.

This is the code i was using.

        public string GetLocalDbFilePath(string filename)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            return Path.Combine(path, filename);
        }

EDIT: The exception being thrown wasn't actually an error, apparently it is normal if the file needed to be created. I had a completely different problem that was causing it to explode.

sk1llsh0t commented 4 years ago

@Kilowhisky With the enforcement of SAF in Android 10, it isn't possible to access folders outside of your apps scoped storage location without the user granting your app permission to access the folder. In my app, I inform the user of the location my app needs access to and throw up a SAF permission prompt. if the user doesn't pick the right place with the apps files, i prompt them again until they pick the right location. Even after granting access, the SAF APIs are terribly implemented. Many 3rd party packages don't play nice with SAF either (i.e. sqlite-pcl-net).

I would highly recommend sticking to scoped storage locations for all your apps files. This way, you don't have to change anything in your app regarding how you access the files (i.e. the code snippet you pasted won't have to change).

If SAF can't be avoided, there may be SAF helper packages on nuget that you could look into. They may make SAF file access a little more bearable.

Shame on google for how they implemented SAF.

sk1llsh0t commented 4 years ago

The other option you have is to target android 9 and your app will continue to work without issues. The downside is you'll lose access to new Android features, bug fixes, etc. Also, if your app is in the Play Store, they won't allow any updates to your app if it isn't targeting at least Android 10 past Nov 2, 2020.

pushpa66 commented 3 years ago

Found a solution after wasting two days. You can use below line of code if you are working on .Net Core API project System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);