2021年3月10日星期三

Xamarin.Forms - How to define scoped storage for android and how can I implement it to downlaod files in Downloads directory?

In my app I have given facility to download reports in PDF formats. Everything was good until I was testing my app on Android 10 and below. Recently I updated a device to Android 11 and now I am not able to download any file in shared storage. I am getting Access Denied error while downloading file.

I have given permission to read and write external storage

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

and my download path is

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

I have also set android:requestLegacyExternalStorage="true" in AndroidManifest.xml

My code to download file looks like this

public async void DownloadSupplierSample(object sender, EventArgs e)      {          try          {              string basepath;              if (Device.RuntimePlatform == Device.Android)                  basepath = DependencyService.Get<IDownloadPath>().GetDownloadsPath();              else                  basepath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "..", "Library");                PermissionStatus readstatus = await Permissions.CheckStatusAsync<Permissions.StorageRead>();              PermissionStatus writestatus = await Permissions.CheckStatusAsync<Permissions.StorageWrite>();                if (readstatus == PermissionStatus.Granted && writestatus == PermissionStatus.Granted)              {                  DownloadSupplier(basepath);              }              else              {                  var read = await Permissions.RequestAsync<Permissions.StorageRead>();                  var write = await Permissions.RequestAsync<Permissions.StorageWrite>();                    if (read == PermissionStatus.Granted && write == PermissionStatus.Granted)                  {                      DownloadSupplier(basepath);                  }              }              await DisplayAlert("", "Sample is downaloaded at Downloads directory","Ok");          }          catch (Exception ex)          {          }      }        async void DownloadSupplier(string basepath)      {          using (var stream = await FileSystem.OpenAppPackageFileAsync("SupplierMaster.xlsx"))          {              string filename = $"SupplierMaster_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";              string filepath = Path.Combine(basepath, filename);                byte[] bytes = Utils.Common.StreamToBytes(stream);              File.WriteAllBytes(filepath, bytes);              //using (Stream filestream = File.Create(filepath))              //{              //    //stream.Seek(0, SeekOrigin.Begin);              //    stream.CopyTo(filestream);              //}          }      }  

I have tried setting flag from device as well from this link

How can I define scoped storage for my app ?

Any update how can I download PDF file in my downloads directory ?

Thank you.

https://stackoverflow.com/questions/66560359/xamarin-forms-how-to-define-scoped-storage-for-android-and-how-can-i-implement March 10, 2021 at 03:44PM

没有评论:

发表评论