In the Xamarin.Forms application we are developing, called "myApp", targeting Android devices only, we need to be able to read from (and possibly write to) the text file
storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt
which in the Windows 10 development platform appears as
This PCDEVICE TYPEInternal shared storageAndroiddatacom.myApp.configuration
In the AndroidManifest.xml file for the project, we have included
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" ... >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
</manifest>
This is confirmed in Visual Studio 2022 by navigating to
Project > myApp.Android Properties > Android Manifest > Required permissions:
and observing the required permissions are asserted.
When the application is first started in the debug mode, in MainActivity.cs
AndroidX.Core.App.ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage)
returns "false", so we invoke
AndroidX.Core.App.ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.ReadExternalStorage, Android.Manifest.Permission.WriteExternalStorage, Android.Manifest.Permission.ManageExternalStorage }, 0)
Android shows a (non-modal) dialog
for which we select "ALLOW".
Later, the code
File.Exists("/storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt")
returns "true"
but the code
mystreamreader = new StreamReader("/storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt");
throws the exception with Message value of
"Access to the path "/storage/emulated/0/Android/data/com.myApp.configuration/myAppStuff.txt" is denied."
A later check shows that the code
AndroidX.Core.App.ActivityCompat.CheckSelfPermission(this, Android.Manifest.Permission.ManageExternalStorage)
returns the value "false".
Why?
2
Answers
Which Android version? As of Android13, EXTERNAL_STORAGE is basically gone. CheckSelfPermission for it will auto-answer false, just as if you answered "No and do not ask again" before. They want you to move to different APIs, e.g. ask a Document Picker to get arbitrary files or be a Document Provider to provide them.
I can’t reproduce your problem. But you said:
So I created a sample to read and write the text file:
In my AndroidManifest.xml:
Please make sure the package’s value in your AndroidManifest.xml file is
com.myApp.configuration
.And the code about create, read and write the file: