title: Xamarin.Forms - How to define scoped storage for android and how can I implement it to downlaod files in Downloads directory?
body: <p>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 <code>Access Denied</code> error while downloading file.</p>
  <p>I have given permission to read and write external storage</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  </code></pre>
  <p>and my download path is</p>
  <pre><code>Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
  </code></pre>
  <p>I have also set <code>android:requestLegacyExternalStorage=&quot;true&quot;</code> in <code>AndroidManifest.xml</code></p>
  <p>My code to download file looks like this</p>
  <pre><code>public async void DownloadSupplierSample(object sender, EventArgs e)
      {
          try
          {
              string basepath;
              if (Device.RuntimePlatform == Device.Android)
                  basepath = DependencyService.Get&lt;IDownloadPath&gt;().GetDownloadsPath();
              else
                  basepath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), &quot;..&quot;, &quot;Library&quot;);
  
              PermissionStatus readstatus = await Permissions.CheckStatusAsync&lt;Permissions.StorageRead&gt;();
              PermissionStatus writestatus = await Permissions.CheckStatusAsync&lt;Permissions.StorageWrite&gt;();
  
              if (readstatus == PermissionStatus.Granted &amp;&amp; writestatus == PermissionStatus.Granted)
              {
                  DownloadSupplier(basepath);
              }
              else
              {
                  var read = await Permissions.RequestAsync&lt;Permissions.StorageRead&gt;();
                  var write = await Permissions.RequestAsync&lt;Permissions.StorageWrite&gt;();
  
                  if (read == PermissionStatus.Granted &amp;&amp; write == PermissionStatus.Granted)
                  {
                      DownloadSupplier(basepath);
                  }
              }
              await DisplayAlert(&quot;&quot;, &quot;Sample is downaloaded at Downloads directory&quot;,&quot;Ok&quot;);
          }
          catch (Exception ex)
          {
          }
      }
  
      async void DownloadSupplier(string basepath)
      {
          using (var stream = await FileSystem.OpenAppPackageFileAsync(&quot;SupplierMaster.xlsx&quot;))
          {
              string filename = $&quot;SupplierMaster_{DateTime.Now.ToString(&quot;yyyyMMddHHmmss&quot;)}.xlsx&quot;;
              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);
              //}
          }
      }
  </code></pre>
  <p>I have tried setting flag from device as well from this <a href="https://developer.android.com/guide/app-compatibility/test-debug#:%7E:text=Toggle%20changes%20using%20the%20developer%20options,-Use%20the%20developer&amp;text=enabled%2C%20enable%20them.-,Open%20your%20device%27s%20Settings%20app%20and%20navigate%20to%20System%20%3E%20Advanced,off%20and%20tap%20the%20switch." rel="noreferrer">link</a></p>
  <p>How can I define scoped storage for my app ?</p>
  <p>Any update how can I download PDF file in my downloads directory ?</p>
  <p>Thank you.</p>