if (Android.OS.Environment.IsExternalStorageManager)
var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
Java.IO.File file = new Java.IO.File(path);
if (!file.Exists())
file.Mkdirs();
//store the image
StartActivityForResult(new Intent(Android.Provider.Settings.ActionManageAllFilesAccessPermission), 0);
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hi...!
I don't think the issue was got resolved. Below is the exception when trying to write to a file using this method.
Code:
File.AppendAllText(filePath, "\n" + contents);
Exception:
[AppCenterCrashes] Unhandled Exception from source=AndroidEnvironment
[AppCenterCrashes] System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Pictures/MagicImages/Parameters.txt" is denied.
[AppCenterCrashes] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.192(intptr,intptr,intptr,intptr,intptr)
System.UnauthorizedAccessException: 'Access to the path "/storage/emulated/0/Pictures/MagicImages/Parameters.txt" is denied.'
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Pictures/MagicImages/Parameters.txt" is denied.
[mono-rt] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.192(intptr,intptr,intptr,intptr,intptr)
[mono-rt] at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.192(intptr,intptr,intptr,intptr,intptr)
[BpBinder] onLastStrongRef automatically unlinking death recipients: <uncached descriptor>
Please make sure you've added the android.permission.READ_EXTERNAL_STORAGE
permission to your project. And add android:requestLegacyExternalStorage="true"
the setting to the AndroidManifest.xml. Because applicadtions use the scoped storage in Android 11, set requestLegacyExternalStorage
to true
could enable your app continue to behave as expected on devices that run Android 10.
I The manifest is already having both Read and Write permissions., along with LegacyExternalStorage = true. But it's still the same issue.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:requestLegacyExternalStorage="true"
As far as I know, this LegacyExternalStorage will be ignored in Android 11.
Basically, I had identified the actual issue in this exception. Am trying to write Log files, which was working till API 29, under Pictures folder. With API 30, I see that the .txt files are not being created in DirectoryPictures. So, with all your suggestions, I had modified the DirectoryPictures to DirectoryDocuments and now the same code works. Hope for time being, will go ahead with this change.
I have a scenario and I tested it. It's failing with UnAuthorized Exception.
With all the permissions granted, write a file to Documents folder in Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).
Delete the app on the phone and debug the same code again.
When reading the file, the app is crashing with UnAuthorized exception.
File.ReadAllText(filePath);
Now manually delete the file from the device.
Write the file using the same app. You see that the file created and path is accessible.
The file which is created by the previous instance of the app from Documents folder is not accessible. But if the same app is creating the file, the path is accessible.
As my app has to write the file to Documents folder, which needs to be read even after installation/reinstallation, I was writing the details to Documents folder.
Hi,PrasanthVaranasi-6196. Have you requested MANAGE_EXTERNAL_STORAGE
permission for your app? As the screenshot shows, the app is allowed to read, modify and delete all the files on the device. I create a file on the device and try to read the file content in code, it works as expected. Do you test the function on a physical device or an emulator? Try to test the code on another device to check the issue.
Yes. I had requested Manage_External_Storage permission.
When you create a file and read it, it works.
The scenario is different and I mentioned the steps.
Read the file from Documents folder if exists, or create it. Read it.
Uninstall the app and installed again in debug mode in VS 2019. Accepted the permissions manually on the physical device.
Now, read the file. Am getting Access denied/unauthorized exception.
I was facing the same issue when using GetExternalStoragePublicDirectory and creating files under /Storage/emulated/0/Documents.
Folder creation is successful, but when creating a new file, getting IOException saying that could not create file and File already exists.
I see the issue is there even when using the https://github.com/xamarin/monodroid-samples/tree/master/LocalFiles repo too.
It worked only for the very first time.
Once the txt file is created, delete the file by going to Files app on Android phone, and try the app again. You will get the same exception.
If the user deletes the file/folder manually, we are getting to see this issue.
Any idea how do we resolve this?
I'd suggest that MANAGE_EXTERNAL_STORAGE is not the way to go.
Google is advising to avoid using MANAGE_EXTERNAL_STORAGE, unless absolutely necessary. See https://developer.android.com/training/data-storage/manage-all-files.
There are easier other alternatives such as the Storage Access Framework or Media Store API.
You'll mostly find that you now don't need any permissions for what you would have been doing previously.