如何使用 xamarin.forms 在 android 中设置外部存储写入运行时权限

您好,在此先感谢。我写这个问题是因为我已经在互联网上搜索了几个星期了,但我仍然找不到问题的答案。我需要在我的 android 应用程序中包含运行时权限,但我找不到可行的方法来执行此操作。我的主要问题是我不知道将 requestPermissions() 函数放在哪里。



慕工程0101907
浏览 258回答 1
1回答

MMMHUHU

首先,您需要在AndroidManifest.xml文件中指定您的应用程序需要的权限<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />之后,您需要确保 Xamarin.Android.Support.Compat NuGet 包包含在您的项目中,因为此包会将权限特定的 API 反向移植到旧版本的 Android。如果您使用的是 Android 6.0 o 之后的版本,您可以使用ContextCompat.CheckSelfPermission方法检查您的 Activity 中的权限或请求if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) == (int)Permission.Granted)&nbsp;{&nbsp; &nbsp; // We have permission.}&nbsp;else&nbsp;{&nbsp; &nbsp; // Storage permission is not granted. If necessary display rationale & request.}
打开App,查看更多内容
随时随地看视频慕课网APP