Android 10 and Scoped Storage (missed features in Unity 2019)

Hello! Android 10 is a big issue for all developers who make app that needs to list files and work with them (file browsers, photo editors and so on). You can read more here: What is Scoped Storage in Android? | Android Central

My problem is that it seems I can no longer access DCIM folder for my app (pixel art editor). The app works fine with Android 9 but it gets UnauthorizedAccessException on Android 10. So my question is how Unity can handle access to DCIM and work with Storage Access Framework APIs in general?

Is the only solution I see now is to wait until someone will write a native android plugin? Unfortunately, I’m not a Java-developer at all.

Same issue here, a plugin seems like the way to go

Currently you can avoid this temporarily by adding this to the manifest

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>

source: Data and file storage overview  |  Android Developers

3 Likes

We got the same issue, but adding the android:requestLegacyExternalStorage="true" to the AndroidManifest.xml did not fix the issue.

We are trying to access the file file:///storage/emulated/0/DCIM/Camera/file.mp4 via the Unity VideoPlayer

This related post confirms that others have the issue too with Unity although the requestLegacyExternalStorage key is defined:

@hippogames : thanks to your post, the article mentions an even worse thing: Android 11 even removes/ignores the requestLegacyExternalStorage for security reasons and only allows file access via Storage Access Framework (SAF) / Scoped Storage.

I already filed an issue at Unity, because they have to get around it directly.

We have the same issue. Do you get any feedback so far?

I’ve had some experience with Storage Access Framework (1, 2, 3) and in my opinion, Unity can’t do anything about this issue. File API is restricted by the Android OS itself for security reasons. When using SAF to access files that are not owned by the app, a confirmation dialog is presented to the user. Only then access to the file is granted (this is why SAF is more secure).

You might ask then why Unity doesn’t display this confirmation dialog automatically whenever we are trying to access a file in external storage. One possible reason is that SAF doesn’t use raw file paths, here’s an example SAF path: content://com.android.externalstorage.documents/tree/primary%3A/document/primary%3APictures. The issue here is that, Android doesn’t have an API to convert a file path to SAF path or vice versa (you can find workaround solutions on stackoverflow but they won’t work in all circumstances). So, Unity can’t reliably convert a file path to SAF path, which is needed in Storage Access Framework.

P.S. You might find these upcoming changes interesting, though:

Thank you for the info.

We are saving and loading only files from within the application.persistentDataPath, other files are working but only the videoplayer url will not work for this.

Do you know a way of handling that, e.g. convert a local path to an SAF compatible path (only for the application.persistentDataPath). Maybe the videoplayer will work with that?

AFAIK, Android doesn’t have an API to convert a file path to SAF path. If there is a standard SAF format for Application.persistentDataPath, I don’t know about it.

Since Application.persistentDataPath can be accessed via File API, I don’t think this VideoPlayer issue is caused by SAF. You can try starting your path with “file://” prefix. If you are using the StreamingAssets folder, then you are working with Application.streamingAssetsPath, not Application.persistentDataPath. Let me know if this is the case.

Has anybody solve this? my phone automatically upgraded to Android 10 last night, now my app cannot access

/storage/emulated/0/DCIM/Camera/20200704_230148.mp4

anymore so the upgrade broke my app in the play store. I cant see how google justifies doing this, should they be sued? Even if i fix it somehow, i can expect it will break again for android 11, and 12, and 13 etc, totally ridiculous, WTF

Looking at this topic, I’d say that you’ll again be able to access that path starting with Android 11. To support Android 10 as well (i.e. support all Android versions), you should set requestLegacyExternalStorage to true in AndroidManifest.xml as suggested in the topic.

1 Like

I tested requestLegacyExternalStorage, but it does not seem to work correctly. Does anybody have luck with this? This property is also not available if targeting other SDK levels than 9 or 10.

Hey @yasirkula , do you know any updates on this? Any info or plugin on how to use Scoped Storage on Unity?

I use some of your plugins so I trust you have much more knowledge on this matter than most Unity users :slight_smile:

You can access images and videos in the Gallery using File API as stated here. To browse other files, you have to either create a native plugin or use an existing plugin like SimpleFileBrowser or NativeFilePicker.

Thanks for the reply! Regarding images/videos in the Gallery, are you aware of a Unity solution?

I currently use your UnityNativeGallery* and it stopped working for Samsung devices with Android 10. Now I have just rolled a test update with that AndroidManifest flag and it’s working again, but it seems (from your message) that this flag is just a temporary hack, on Android 11 I will have to find a definitive fix. If you have any ideas let me know. Thanks!!

  • EDIT: I use an older version of your plugin, I dont update it for over a year, maybe the current version has a fix for this already?

Yes I’d recommend you to update the plugin and give it another try.

What do you mean by a Unity solution for Gallery? Like NativeGallery but made by Unity? If so, AFAIK it doesn’t exist.

No I mean a C#/Unity solution instead of a Java/“core Android” solution. I saw that the link you posted is for the Android docs, and usually the solutions there dont apply for the Unity world. Is the current solution for this problem doable in Unity-only world (without the need to write Java code)?

To be honest I took a quick look on the Android docs link but I didnt understand much what I need to do to use this scoped storage thing to access the album photos.

You need to call Environment functions using AndroidJavaClass to retrieve special folders’ paths but then, you can use System.IO.File API to browse images and videos inside these folders. You need to keep the requestLegacyExternalStorage flag in AndroidManifest for Android 10 compatibility because File API compatibility was introduced in Android 11 and is otherwise not backwards-compatible.

2 Likes

Well, a lot of apps will stop working… Google loves making our life worse…
Probably Unity can make something to help, creating a function that returns that folder where all other apps can have access to. My game is heavly dependent on user mods (vehicles, repaints, sounds, maps…). It is already hurting.
Following the thread to see any news about it, I would love to see a solution asap since this is very critical.

Why do other apps need to have access to your mod files, can’t you simply use Application.persistentDataPath? If you really need to interact with public directories, you’ll need to use Storage Access Framework; I’m not aware of any public directories that you can read/write generic files using File API.

The plugins I’ve mentioned here use Storage Access Framework, so you can check them out if you want: Android 10 and Scoped Storage (missed features in Unity 2019)

1 Like

My users are used to use file managers, text editors, image and audio editors to change everything on the vehicles and maps. So it is convenient that the folder is freely available. They download mods via browser and extract to a folder, being able to change almost anything there.

If I have to make an UI to manage it, the cost of development will increase a lot, since every sound, txt and image must be replaced one by one, it would be like creating a file manager… Also for them to create backups of installed mods will be much harder or almost impossible. I can’t believe that it is happening. :frowning:

Wouldn’t my SimpleFileBrowser plugin be useful in this case? Your users would just select the mod’s folder with the plugin and then you’d use FileBrowserHelpers functions (similar to File API) to load these files.

1 Like