IsExternalStorageManager == true,但访问外部存储被拒绝 (Android 11)

IsExternalStorageManager == true, but access to external storage denied (Android 11)

我有这样一个代码,我在其中获得了对外部存储的写入权限。那里我也检查是否获得了这个权限。

private void ExternalStoragePermission()
    {

        ActivityCompat.RequestPermissions(this,
                    new String[]{Manifest.Permission.ReadExternalStorage,
                            Manifest.Permission.ManageExternalStorage}, 1);

        //Android 11
        if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)
        {
            if (!Android.OS.Environment.IsExternalStorageManager)
            {
                try
                {
                    Intent intent = new Intent(Android.Provider.Settings.ActionManageAllFilesAccessPermission);
                    intent.AddCategory("android.intent.category.DEFAULT");
                    intent.SetData(Android.Net.Uri.Parse(String.Format("package:%s", new object[] { Application.Context.PackageName })));
                    StartActivityForResult(intent, 2000);
                }
                catch (Exception e)
                {
                    Intent intent = new Intent();
                    intent.SetAction(Android.Provider.Settings.ActionManageAllFilesAccessPermission);
                    StartActivityForResult(intent, 2000);
                }
            }
        }
        //Below Android 11
        else
        {
            if ((ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
            || (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted))
            {
                ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
            }
        }
    }

即使我有这个权限,我仍然无法上传文件到外部存储

我没有想法,请帮帮我

Android 10 为应用程序引入了一种新的存储模式,称为范围存储,它改变了应用程序在设备外部存储上存储和访问文件的方式。如果您的目标是 Android 10(API 级别 29)或更高,请在应用的清单文件中将 requestLegacyExternalStorage 的值设置为 true。

<application android:requestLegacyExternalStorage="true" android:label="FormsSample.Android" android:theme="@style/MainTheme"></application>

然后在AndroidManifest.xml中添加READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限

最后,我做了一个示例,将 txt 文件保存在下载文件夹中。

public class MainActivity : AppCompatActivity
{
    private Button btn1;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        btn1 = FindViewById<Button>(Resource.Id.button1);
        btn1.Click += Btn1_Click;

    }

    private void Btn1_Click(object sender, System.EventArgs e)
    {
        requestpermission();
        CreateFile("test.txt");
    }

    public void CreateFile(string FileName)
    {
        string text = "hello world";
        byte[] data = Encoding.ASCII.GetBytes(text);
        string rootPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
        var filePathDir = Path.Combine(rootPath, "folder");
        if (!File.Exists(filePathDir))
        {
            Directory.CreateDirectory(filePathDir);
        }
        string filePath = Path.Combine(filePathDir, FileName);
        File.WriteAllBytes(filePath, data);
    }
    public void requestpermission()
    {
        if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 1);
        }


        if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
        {
            ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, 1);
        }
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
}