I'm making a new cross-platform MAUI App,and I tried to simply create a json file.the source code looks like this:
private async void WriteSomething()
{
#if ANDROID
FileStream fs = new(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) "/test.json", FileMode.Create);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
await sw.WriteAsync(str);
sw.Close();
fs.Close();
#elif WINDOWS
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) "\\test.json";
FileStream fs = new(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
await sw.WriteAsync(str);
sw.Close();
fs.Close();
#endif
}
The program runs well on windows machine,but the program throws System.UnauthorizedAccessException on android emulator. I've searched this problem on StackOverflow,but most of the questions about this is on Xamarin platform.(Xamarin.Android or Xamarin.Forms)
According to the answers,I should request the storage permission like this:
First,add the following code to my AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Second,check if the target android version is Android M or above.If the answer is true,then invoke RequestPermissions method:
#if ANDROID
private void CheckAppPermissions()
{
if ((int)Build.VERSION.SdkInt < 23)
{
return;
}
else
{
if (PackageManager.CheckPermission(Manifest.Permission.ReadExternalStorage, PackageName) != Permission.Granted
&& PackageManager.CheckPermission(Manifest.Permission.WriteExternalStorage, PackageName) != Permission.Granted)
{
var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
RequestPermissions(permissions, 1);
}
}
}
#endif
However,I found there is no such method in Android namespace.
CS0103 the name "RequestPermissions" does not exist in the current context.
I guess it exists only in Xamarin platform.Or it acctually exists in another namespace.And that means I can't get the read and write permission I need.
A brand-new solution of the permission problem is needed.
**Note:**My English is poor.And I have little programming experience.Please forgive me.
Thanks in advance.
CodePudding user response:
Actually, if you just operate the file in the System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
path, it shouldn't need any storage permissions. Because it's the app's own private folder. And I have tested your code in a new project, the josn file was created successfully.
In addition, if you want to request the permission in the maui, you need to use the api in the maui. You can use the following code instead of the yours.
PermissionStatus statusread = await Permissions.RequestAsync<Permissions.StorageRead>();
PermissionStatus statuswrite = await Permissions.RequestAsync<Permissions.StorageWrite>();
Finally, you can also use the permission Mahesh mentioned, but it should be used if necessary. And if you still want to it, you can try to add the following code in the MainActivity to grant the permission by the user.
protected override void OnCreate(Bundle savedInstanceState)
{
if (!Android.OS.Environment.IsExternalStorageManager)
{
Intent intent = new Intent();
intent.SetAction(Android.Provider.Settings.ActionManageAppAllFilesAccessPermission);
Android.Net.Uri uri = Android.Net.Uri.FromParts("package", this.PackageName, null);
intent.SetData(uri);
StartActivity(intent);
}
base.OnCreate(savedInstanceState);
}
CodePudding user response:
use this permission, hope it's helps, check once with give manually permission.
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />