简体   繁体   English

以 ZE84E30B9390CDB64DB6DB2C9AB8784 为目标时,将 PDF 保存在 Xamarin Forms 中

[英]Save PDF in Xamarin Forms when targeting Android 11

I want a method to save PDF that coming as a byte[] from the server.我想要一种方法来保存来自服务器的字节 [] PDF。 iOS working well. iOS 运行良好。 In Android, I need to save the PDF in my app specific storage location.在 Android 中,我需要将 PDF 保存在我的应用程序特定存储位置。 Previously I saved in the common external storage location.以前我保存在通用的外部存储位置。 With the new android policy I cannot do that.使用新的 android 政策,我无法做到这一点。 All my updates are rejected from the play store.我的所有更新都被 Play 商店拒绝。 Is there a way to use Xamarin.Essential for save the PDF?有没有办法使用 Xamarin.Essential 来保存 PDF? Can we use Directory.CreateDirectory in Android 11 now?我们现在可以在 Android 11 中使用Directory.CreateDirectory吗?

Code for saving the file..保存文件的代码..

   public class AndroidSaveFile : ISaveFile
    {
        public async Task<string> SaveFiles(string filename, byte[] bytes, string filePath)
        {
            var status = await CrossPermissions.Current.CheckPermissionStatusAsync<StoragePermission>();
            if (status != PermissionStatus.Granted)
            {
                if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
                {
                    await Application.Current.MainPage.DisplayAlert("Storage permission!", "To save files, you need the storage permissions.", "OK");
                }

                status = await CrossPermissions.Current.RequestPermissionAsync<StoragePermission>();
            }

            if (status == PermissionStatus.Granted)
            {
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                File.WriteAllBytes(filename, bytes);
            }
            else if (status != PermissionStatus.Unknown)
            {
                await Application.Current.MainPage.DisplayAlert("Storage Denied!", "Can not access storage on this device. Please check the permission and try again.", "OK");
            }

            return filename;
        }
    }

According to the native docs, we could not create our own directory on external storage.根据本机文档,我们无法在外部存储上创建自己的目录。

Starting in Android 11, apps cannot create their own app-specific directory on external storage.从 Android 11 开始,应用程序无法在外部存储上创建自己的应用程序特定目录。 To access the directory that the system provides for your app, call getExternalFilesDirs().要访问系统为您的应用程序提供的目录,请调用 getExternalFilesDirs()。

For more details about, please check the Storage updates in Android 11:https://developer.android.com/about/versions/11/privacy/storage有关更多详细信息,请查看 Android 11 中的存储更新:https://developer.android.com/about/versions/11/privacy/storage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM