Xamarin-System.UnauthorizedAccessException:对路径的访问被拒绝 [英] Xamarin-System.UnauthorizedAccessException: Access to the path is denied

查看:37
本文介绍了Xamarin-System.UnauthorizedAccessException:对路径的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载文件,但出现 System.UnauthorizedAccessException:对路径/storage/emulated/0/Download/test.pdf"的访问被拒绝.我已经在 Android Manifest 文件中设置了所需的权限.

I'm trying to download a file and I'm getting System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Download/test.pdf" is denied. I have set required permission in Android Manifest file.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

下载路径:

Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)

如果我使用下面的路径作为我的下载路径,我可以下载文件.但我无法将 PDF 文件共享到谷歌驱动器、投递箱或任何其他 System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

If i use the below path as my download path i can able to download the file. But i cant able to share the PDF file to google drive, drop box or any other System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

我使用的是 Xamarin.Forms v2.4.0.282 和 Xamarin.Android.Support 包 v25.4.0.2.

I am using Xamarin.Forms v2.4.0.282 and Xamarin.Android.Support packages v25.4.0.2.

注意:当使用 Xamarin.Forms 版本 2.3.5.256-pre2 和 Xamarin.Android.Support 包 v23.3.0 时,代码运行良好请提出您解决问题的想法.

Note: The code was woking fine when use Xamarin.Forms version 2.3.5.256-pre2 and Xamarin.Android.Support packages v23.3.0 Please suggest your ideas to resolve the issue.

提前致谢.

推荐答案

根据您使用的 Android 版本,即使在 6.0 或更高版本的清单中添加了权限,用户也必须在应用运行时显式启用该权限并且在安装过程中要求较低版本的权限.例如,在应用程序启动时,我创建了一个方法来检查它是否已启用,如果未启用则请求权限.

Depending on the version of Android you are using even with the permissions added in the manifest in 6.0 or up the user has to explicitly enable the permission when the app runs and on lower versions permission is asked during install. For example, on startup of the app I created a method to check if it is enabled and request permission if it's not.

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);
        }
     }
}

您也可以使用支持库来执行此操作,这样更简单,而且您无需检查 Android 版本.有关详细信息,请查看 google 的文档.

You can also use the support library to do this, which is simpler and allows you to not have to check the Android version. For more info check out google's documentation.

这篇关于Xamarin-System.UnauthorizedAccessException:对路径的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆