Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write to Android External storage even with permissions set

In my Android 10.0 (Q) app using Xamarin Forms I have correctly set in my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest **** />
    <application *** ></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

I have permissions granted in via code

var permission = ActivityCompat.CheckSelfPermission(MainActivity.Instance, Manifest.Permission.WriteExternalStorage);

if (permission != Permission.Granted)
{
    ActivityCompat.RequestPermissions(
      MainActivity.Instance, 
      new[] 
      { 
         Manifest.Permission.WriteExternalStorage, 
         Manifest.Permission.ReadExternalStorage 
      }, 1);
}

Yet when I try to create a file in my documents or pictures folder I get a permissions denied error: Java.IO.FileNotFoundException: '/storage/emulated/0/Documents/file.txt: open failed: EACCES (Permission denied)' What am I missing?

var folder= Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments);
folder.Mkdirs();

var filePath = Path.Combine(folder.AbsolutePath, filename);

// I get a permissions error here 
using (var stream = new Java.IO.FileOutputStream(filePath))
{
     await stream.WriteAsync(data).ConfigureAwait(false);
}
like image 808
Michael S. Scherotter Avatar asked Mar 06 '20 17:03

Michael S. Scherotter


People also ask

How do I get permission to write to external storage on Android?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

What is storage permission not granted?

If permission isn't granted, no files stored locally on the device can be indexed and added to the library database. You can verify that the storage permission has been denied in the app details page in Android Settings as "No permissions granted" will appear in the Permissions section.

How do I get internal storage permissions on Android?

Storage permissions Android defines two permissions related to storage: READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE . As you can see, permissions are only defined for accessing external storage. That means that every app, by default, has permissions to access its internal storage.


1 Answers

Before your app is fully compatible with scoped storage, you can temporarily opt out based on your app's target SDK level or the requestLegacyExternalStorage manifest attribute:

Google has a new feature on Android Q: filtered view for external storage. A quick fix for that is to add this code in the AndroidManifest.xml file:

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
    Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
   ...
 </application>
</manifest>
like image 72
Leo Zhu - MSFT Avatar answered Oct 14 '22 15:10

Leo Zhu - MSFT