Scoped internal Storage access error on Android 11+: open failed: EACCES (Permission denied)

264 views Asked by At

I'm trying to access to public Document directory by this method:

public static String getInternalRemovablePath() {
    return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
}

and then I create a directory inside it called GT-backup then save some files over there.

On the first try everything works fine. The directory GT-backup was created and I can access files inside it and edit them.

But whenever the user decides to clear app data or uninstall and install the app again => then when the app tries to access the same already existing directory that my app owns Gt-backup error says: open failed: EACCES (Permission denied).

If the user manually deletes the directory GT-backup inside the Documents directory, everything works again and the error disappears.

I think this case happening on Android 11 because the system decided that my app doesn't own the directory GT-backup even though my app created it before uninstalling it.

Any ideas to make Android know that this directory is created by the same app before uninstalling?

I tried to clear data and uninstall.. the same scenario happened, I'm assuming that if the app is re-installed the app, then I can access the same created directory by the same app.

4

There are 4 answers

0
CommonsWare On

Any ideas to make Android know that this directory is created by the same app before uninstalling?

That is not an option, sorry. Consider using the Storage Access Framework instead.

2
anemomylos On

You could use the MANAGE_EXTERNAL_STORAGE permission, but be prepared to face rejection by Google if you distribute the application in the Play Store. More here: https://developer.android.com/training/data-storage/manage-all-files

0
Anggrayudi H On

You have to use Storage Access Framework (SAF) with scoped storage restriction. I have a library that ease and simplify SAF usage, i.e. SimpleStorage

FYI, MANAGE_EXTERNAL_STORAGE is not a good solution, unless you're creating file manager, antivirus, and file specific related apps. Otherwise, your app will be rejected by Play Store.

0
Amer Alzibak On

it's not a perfect idea, but solved this case by telling user to delete the file "GT-backup" manually and back to the app, this is required by my app business case, we don't need user to decide where to put the directory manually by (SAF).