更新时间:2023-11-29 14:51:58
您正面临权限问题.
首先,您必须添加AndroidManifest:
First, you will have to add in your AndroidManifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并且由于Android Marshmallow,您需要向用户询问权限,因此,我建议使用软件包权限插件
And since Android Marshmallow, you need to ask the user for the permissions, so I advise to use the package Permissions.Plugin
并添加您的MainActivity:
And add in your MainActivity:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
您可以通过以下方式签入运行时是否具有权限:
You can check in runtime if you have the permissions by:
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
if (status != PermissionStatus.Granted)
{
if(await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
{
await DisplayAlert("Need storage, "Request storage permission", "OK");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
//Best practice to always check that the key exists
if(results.ContainsKey(Permission.Storage))
status = results[Permission.Storage];
}
有关更多信息,您可以查看此博客文章,其中解释了Android中的所有权限- https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/
For further information you can check this blog post explaining all the permissions in Android - https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/