UnauthorizedAccessException while accessing file using FileStream in Android #24745
Replies: 4 comments
-
Hi I'm an AI powered bot that finds similar issues based off the issue title. Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you! Open similar issues:
Closed similar issues:
|
Beta Was this translation helpful? Give feedback.
-
Hey, Im no expert, but Im pretty sure Android 10 changed the way you can access files external to the application. More info here: https://stackoverflow.com/questions/60415859/is-it-possible-to-programmatically-access-download-folder-on-android-q-sdk-2 I think the recommended way is to use FileSystem.AppDataDirectory or FileSystem.CacheDirectory to work with files. If you need to open external folders, I think you need to gain explicit permission from the user, using the DocumentsProvider (which I had to do once, and will avoid in the future, its clunky and not user friendly...) |
Beta Was this translation helpful? Give feedback.
-
Thanks for your suggestion. I have tried below things but not works
End requirement: We need to access document from mobile storage using FileStream overload |
Beta Was this translation helpful? Give feedback.
-
FileStream works fine with appdata and cachedirectory, but if you need to store these files so that users can access them outside the app, you need to let user explicitly select the folder where you will store the file, using scoped storage: https://source.android.com/docs/core/storage/scoped In android 10, it is possible to opt out of this new model, using legacy storage mode, but its just temporary and wont work in android 11+: https://developer.android.com/reference/android/R.attr#requestLegacyExternalStorage |
Beta Was this translation helpful? Give feedback.
-
Description
When opening file that exist in Download folder of Android device, it throws System.UnauthorizedAccessException: 'Access to the path '/storage/emulated/0/Download/HelloWorld/docx' is denied'
Steps to Reproduce
Call this code from "MainPage.xaml.cs"
In the android device, maintain a file "HelloWorld.docx" inside Download folder.
Build and deploy the app in Android device.
Checked whether file existing using below code examples, both return true only
Java.IO.File file = new Java.IO.File(folderPath + filename); if (file.Exists())
Another way:
bool isExist = File.Exists(folderPath + filename);
Note: This issue not occurs while writing a file using same path and below code. So, issue in opening only
using (FileStream destStream = new FileStream(relativePathOutput, FileMode.Create, FileAccess.ReadWrite))
Link to public reproduction project repository
No response
Version with bug
8.0.80 SR8
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 10.0
Did you find any workaround?
Suggestion tried:
Relevant log output
Beta Was this translation helpful? Give feedback.
All reactions