【发布时间】:2022-01-18 13:39:23
【问题描述】:
我想要一种方法来保存来自服务器的 byte[] 格式的 PDF。 iOS 运行良好。在 Android 中,我需要将 PDF 保存在我的应用程序特定的存储位置。以前我保存在通用的外部存储位置。使用新的 android 政策,我无法做到这一点。我的所有更新都被 Play 商店拒绝。有没有办法使用 Xamarin.Essential 来保存 PDF?我们现在可以在 Android 11 中使用 Directory.CreateDirectory 了吗?
保存文件的代码..
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;
}
}
【问题讨论】:
标签: android xamarin.forms