android: System.UnauthorizedAccessException: Access to the path is denied. ---> System.IO.IOException: Operation not permitted
Steps to Reproduce
- Create a Xamarin project for Android
- Ask for ReadExternalStorage and WriteExternalStorage permissions
- Try simple operation using System.IO.File like Copy or Move
Sample code
try
{
string logFolder = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "Download", "logs");
DateTime now = DateTime.Now;
string timestamp = now.ToString("yyyyMMdd_HHmmss");
logFolder = Path.Combine(logFolder, timestamp);
Directory.CreateDirectory(logFolder);
foreach (var logFile in _logFiles)
{
string destFilePath = Path.Combine(logFolder, _systemIo.GetName(logFile));
File.Copy((logFile, destFilePath); //File.Move results in the same error
}
}
catch (Exception ex) { }
Expected Behavior
Step 3 to be executed successfully
Actual Behavior
System.UnauthorizedAccessException: Access to the path is denied. —> System.IO.IOException: Operation not permitted
Sometimes for the same code, I get System.IO.IOException: Sharing violation on path /data/user/0/com.my.project/files/logs/20190801.log
Worked without problem with XamarinAndroid SDK 9.1 and VS 2017
Version Information
Xamarin.Android SDK version 9.4.0.51 VS 2019 version 16.2.0 Android 6 and 7
Log File
System.UnauthorizedAccessException: Access to the path is denied. —> System.IO.IOException: Operation not permitted
at Interop.ThrowExceptionForIoErrno (Interop+ErrorInfo errorInfo, System.String path, System.Boolean isDirectory, System.Func2[T,TResult] errorRewriter) [0x00014] in <ff07eae8184a40a08e79049bbcb31a0e>:0 at Interop.CheckIo (System.Int64 result, System.String path, System.Boolean isDirectory, System.Func2[T,TResult] errorRewriter) [0x0000a] in <ff07eae8184a40a08e79049bbcb31a0e>:0
at Interop.CheckIo (System.Int32 result, System.String path, System.Boolean isDirectory, System.Func`2[T,TResult] errorRewriter) [0x00000] in <ff07eae8184a40a08e79049bbcb31a0e>:0
at System.IO.FileSystem.CopyFile (System.String sourceFullPath, System.String destFullPath, System.Boolean overwrite) [0x00052] in <ff07eae8184a40a08e79049bbcb31a0e>:0
at System.IO.FileSystem.LinkOrCopyFile (System.String sourceFullPath, System.String destFullPath) [0x00065] in <ff07eae8184a40a08e79049bbcb31a0e>:0
at System.IO.FileSystem.MoveFile (System.String sourceFullPath, System.String destFullPath) [0x0003a] in <ff07eae8184a40a08e79049bbcb31a0e>:0
at System.IO.File.Move (System.String sourceFileName, System.String destFileName) [0x00083] in <ff07eae8184a40a08e79049bbcb31a0e>:0
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 56 (19 by maintainers)
Thanks for the additional action on this Issue. I just want to point out that although replacing the File.Copy and File.Move with Golemanova’s workaround above will work for code that we control, it doesn’t fix the problem where File.Copy and File.Move are used within 3rd party dependencies and nuget packages included in the project. So we may still get unexpected behaviour on android even though there is no code we can easily change.
SubramanymRajuB, you have completely missed the point. It is/was a bug in Mono, nothing to do with Android permissions.
If you are still getting UnauthorizedAccessException for write or read file in Xamarin Android. I just written article to resolve it http://bsubramanyamraju.blogspot.com/2019/12/resolved-unauthorizedaccessexception.html
Thanks for the additional testing!
Investigation update with some new information:
This issue appeared because
File.Copy()andFile.Move()now explicitly set the read, write, and execute permission mode bits for user, group, and other on the destination file to match the source file. The problem is that Android has special behavior for how file ownership and permissions work on external storage:File.Copy()throws an exception.fchmod()system call returns0(success), but the permission mode bits don’t actually change. This allowsFile.Copy()to succeed.I have submitted https://github.com/mono/mono/issues/17133 with additional details about the unusual way that permissions behave in the external storage directory on Android so that the Mono and CoreFX teams can consider whether
File.Copy()should add special handling for it.Suggested workaround for now
The workaround Golemanova suggested is one good way to avoid the problems caused by the special behavior of permissions on Android external storage.
Golemanova commented on Aug 14 https://github.com/xamarin/xamarin-android/issues/3426#issuecomment-520790110
Release status update
A new Release version has now been published on Windows that includes the fix for this item. The fix is not yet published in a Release version on macOS. I will update this item again when a Release version is available on macOS that includes the fix.
Fix included in Xamarin.Android 10.1.0.30.
Fix included on Windows in Visual Studio 2019 version 16.4. To get the new version that includes the fix, check for the latest updates or install the latest version from https://visualstudio.microsoft.com/downloads/.
(Fix also included on macOS in Visual Studio 2019 for Mac version 8.4 Preview 2.1 and higher. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.)
@ravinderjangra, yes, that sounds like a different behavior. Specifically, it sounds like the new scoped storage behavior of Android 10:
https://developer.android.com/training/data-storage/#scoped-storage
It sounds like the Download directory would be outside of the app-specific external storage directory, so it wouldn’t be included in the app’s external storage scope.
One way to confirm that scoped storage is involved would be to disable it temporarily: https://developer.android.com/training/data-storage/compatibility. (Note that you’ll need to set Compile using Android version to Android 10.0 under the Application tab of the project property pages to use the
android:requestLegacyExternalStorageattribute.)If that doesn’t stop the exception, then if you could submit a new issue with a snippet of the code you’re using so the team can take a look, that would be perfect. Thanks!
Release status update
A new Release version has now been published on macOS that includes the fix for this item.
Fix included in Xamarin.Android 10.1.1.0.
Fix included on macOS in Visual Studio 2019 for Mac version 8.4. To get the new version that includes the fix, check for the latest updates on the Stable updater channel.
(Fix also included on Windows in Visual Studio 2019 version 16.4 and higher. To get the new version that includes the fix, check for the latest updates or install the latest version from https://visualstudio.microsoft.com/downloads/.)
Release status update
A new Preview version has now been published that includes a fix for this item. The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.
Fix included in Xamarin.Android 10.1.0.30.
Fix included on Windows in Visual Studio 2019 version 16.4 Preview 5. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.
Fix included on macOS in Visual Studio 2019 for Mac version 8.4 Preview 2.1. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.
Verification status: Verified resolved in Xamarin.Android 10.1.0.30.
To verify the fix, I repeated the steps I ran in https://github.com/xamarin/xamarin-android/issues/3426#issuecomment-534423074.
I was able to reproduce the exception as before when using Visual Studio 2019 version 16.4 Preview 3 with Xamarin.Android 10.1.0.29.
I was no longer able to reproduce the exception when using Visual Studio 2019 version 16.4 Preview 5 with Xamarin.Android 10.1.0.30. I verified that the desired destination file was present in the external storage location after the call to
File.Copy(). I similarly verified that the desired destination file was present when usingFile.Move()rather thanFile.Copy().If my product had a serious bug like this, I wouldn’t have allowed myself to break for Christmas until I’d fixed it. Perhaps my work ethic is stronger than some at Microsoft.
Seriously, how is this not fixed yet?
@grendello I can reproduce the File.Copy issue on desktop Mono, so it’s likely not specific to Android.
https://github.com/mono/mono/issues/16032
We are now using this instead of File.Copy:
and this instead of File.Move:
@brendanzagaeski We don’t care about the process or what release its scheduled in. Just get it done, it’s been 5.5 months. Its very obvious that the process isn’t working. This should have been fixed last August. A bug in File.Copy deserves a release of it’s own. Do you not understand what a serious bug this is?
Verification status: not yet resolved
Steps followed to test
Test case: FileCopyUnauthorized.zip
Actual results
When the app is built with Xamarin.Android 10.0.0.43 in Visual Studio 2019 version 16.3 or Xamarin.Android 10.0.99.100 in Visual Studio 2019 version 16.4 Preview, an unhandled exception is thrown:
Expected results
When the app is built with Xamarin.Android 9.3.0.23 in Visual Studio 2019 version 16.1.6, no exception is thrown.
The issue depends on the libmonosgen-2.0.so and mscorlib.dll version
Steps tested:
Result:
The
File.Copy()call completes successfully.The issue also depends on the device
Devices where the
UnauthorizedAccessExceptionoccursAndroid 6.0 Marshmallow (API level 23) x86, Google APIs emulator (
system-images;android-23;google_apis;x86) Android 7.1 Nougat (API level 25) x86, Google APIs emulator (system-images;android-25;google_apis;x86) Android 7.1 Nougat (API level 25) x86_64, Google APIs emulator (system-images;android-25;google_apis;x86_64)Devices where the exception does not occur
Android 7.1 Nougat (API level 25) armeabi-v7a, Motorola Moto E4 Android 9.0 Pie (API level 28) x86, Google APIs emulator (
system-images;android-28;google_apis;x86) Android 9.0 Pie (API level 28) arm64-v8a, Google Pixel 3A new Preview version has now been published that includes a fix for this item. The fix is not yet included in a Release version. I will update this issue again when a Release version is available that includes the fix.
Fix included in Xamarin.Android 10.0.0.40.
Fix included on Windows in Visual Studio 2019 version 16.3 Preview 3. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.
Fix included on macOS in Visual Studio 2019 for Mac version 8.3 Preview 3. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.