2

The permissions for accessing shared storage (/storage/emulated/0) are getting really really complicated and changing all the time. As I understand it when targeting Android 11 there is no way to opt out of scoped storage, which means you literally can't ever do fopen("/storage/emulated/0/foo.dat");. Apparently the preferred way to open a file will be with the ACTION_OPEN_DOCUMENT but this just returns a content:// URI, which I can't read using the NDK. I'm also unsure if you get to use that URI forever.

How are NDK libraries meant to access files in /storage/emulated/0 on Android 11?

Timmmm
  • 88,195
  • 71
  • 364
  • 509

2 Answers2

3

Aha, it seems they have moved the permission to read/write all files to MANAGE_EXTERNAL_STORAGE. It's one of those permissions that users have to grant in settings, and there's an intent (ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION) to take people directly there.

However they probably won't let you upload an app to the play store with this permission unless it's a file manager or similar, and you currently can't use it at all.

If your app includes a use case that's similar to the following examples, it's likely to be allowed to request the MANAGE_EXTERNAL_STORAGE permission:

  • File managers
  • Backup and restore
  • Anti-virus apps
  • Document management apps

Due to COVID-19 related considerations, apps that target Android 11 (API level 30) or higher and need the MANAGE_EXTERNAL_STORAGE permission cannot be uploaded to Google Play until early 2021.

I should also note that there seems to be an exception for opening media files only - i.e. files indexed by Android's media scanner.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
0

On Android 11 devices:

All apps can write files in all the public directories like Dowload, Documents, DCIM, Pictures and so on.

If not in the directory itself then in a subdirectory of it which you create first.

For DCIM and Pictures only files with extension like .jpg, .png and .webp are accepted.

More public directories only accept certain extensions.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • Do you have an actual reference for that? It directly contradicts what it says [here](https://developer.android.com/training/data-storage#scoped-storage): `apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created.` – Timmmm Feb 24 '21 at 10:00
  • I'm not going to spend hours setting up code for something that *the documentation says will not work and you have not provided any evidence for*. As I said in my answer, media files are given a special exception and I suspect you are getting confused by that. – Timmmm Feb 24 '21 at 11:14
  • Pretty strange that changing fopen("/storage/emulated/0/foo.dat"); to fopen("/storage/emulated/0/Documents/foo.dat"); would cost you hours. And im not confused. For the rest i just gave an answer to your question. Strange you want more. Just try and confirm. After that we can talk. – blackapps Feb 24 '21 at 11:21
  • I haven't written the code yet. That's why it would take several hours to verify that your solution - that you have provided no evidence for and contradicts the documentation - actually works. – Timmmm Feb 24 '21 at 12:11
  • How would you like me to provide evidence? What do you have in mind? If you think it is not true what i said then try it and come back telling that it did not work. But that will not happen as it will work. – blackapps Feb 24 '21 at 12:33
  • Your code will do. Well if you open in creating mode. (cannot see if you do). You cannot just try to read a file created by another app or copied to that directory by other means. – blackapps Feb 24 '21 at 13:55
  • "You cannot just try to read a file created by another app or copied to that directory by other means." Glad you finally agree! – Timmmm Feb 24 '21 at 21:47
  • Yes, I can confirm that public Download directory is still writable under Android. – Alexander Dyagilev May 06 '21 at 11:47