# Scoped storage with MediaStore

**URL:** <https://qa.fmod.com/t/scoped-storage-with-mediastore/17332>\
**Category:** FMOD Engine\
**Tags:** android\
**Created:** [June 13, 2021, 9:41pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332 "2021-06-13T21:41:19Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![sachinsshah](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@sachinsshah](https://qa.fmod.com/u/sachinsshah)\
**Post date:** [June 13, 2021, 9:41pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/1 "2021-06-13T21:41:19Z")

</div>

Hi team at FMOD, I am wanting to program an Android music player app so am currently exploring some Audio Engines.  
When making an Android app, it’s now recommended to use the **Scoped Storage** framework.  
So to work with **Uri’s** , a **FileDescriptor** can be created and used to read the file (as I have seen with BASS Audio Engine).  
Is this possible for FMOD Audio Engine for Android?  
If so, please can you point me to the documentation regarding this, as I was unable to see it.

Otherwise if it’s not possible, then please can you suggest a workaround. Here are some links that may help:

> <https://stackoverflow.com/questions/56924085/how-can-i-deal-with-content-uri-in-ndk>

(same domain/extension as above)/questions/57445600/ffmpeg-seeking-not-possible-with-file-descriptor-on-android-q  
(same domain/extension as above)/questions/59000390/android-accessing-files-in-native-c-c-code-with-google-scoped-storage-api

If it helps, I have tried with the android:requestLegacyExternalStorage=“true” flag in the manifest and it reads the file fine as I pass a path of that file into the method. But this flag is now not being used in Android as the apps need to target the latest version of Android when uploading to Google Play.  
I have downloaded this project from GitHub and just editing and playing around with it before I’m certain that FMOD does what I am looking for:

> **[iDeMonnnnnn/DeMon\_Sound](https://github.com/iDeMonnnnnn/DeMon_Sound)**
>
> 🎧Android端基于FMOD/SoundTouch的简单音频变声解决方案。. Contribute to iDeMonnnnnn/DeMon\_Sound development by creating an account on GitHub.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![tristanjl](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/tristanjl/32/261_2.png) [@tristanjl](https://qa.fmod.com/u/tristanjl)\
**Post date:** [June 15, 2021, 6:19am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/2 "2021-06-15T06:19:00Z")

</div>

Hi sachinsshah,

Currently we only support uris under the path `file:///android_asset/` using the Asset Manager. Files not under that path are currently opened via fopen, which as you note, is not supported in Android 10/11 for files that you do not have permission to access. I have added a task to add support for Scoped Storage.

---

<div class="post-metadata">

**Author:** ![tristanjl](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/tristanjl/32/261_2.png) [@tristanjl](https://qa.fmod.com/u/tristanjl)\
**Post date:** [November 30, 2021, 11:53pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/3 "2021-11-30T23:53:34Z")

</div>

We did some further investigation to this:

It appears as though, although scoped storage was recommended for API 29, for API 30 onwards, support for fopen was restored assuming that access permissions were obtained. To work with this, it appears as though the requirements are

API 28 and earlier - no new requirements  
API 29 - recommended that you use requestLegacyExternalStorage  
API 30+ you need to ensure you have the required access permission using scoped storage apis. After this, you should be able to use the fmod apis via their file paths as was the case previously.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [December 9, 2021, 1:28pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/4 "2021-12-09T13:28:20Z")

</div>

Is it possible for you to add support for either **Android**'s **document URI** or Java’s **InputStream** to **FMOD\_System\_CreateSound()**?

We were building with target API **29** to support Android 11 devices with the **requestLegacyExternalStorage** option as you suggested above.

But **Google Play Store** now only allows apps built with target API **30** and up, so all the Android developers have to use either **Storage Access Framework** ( **SAF** ) or **Media Store API** to access public directories of Android 10 devices and later ones.

As you said, it’s still possible to access via direct file paths thanks to the **File path API** introduced with **Android 11** , but the problem is now there’s no way to get any absolute paths with **SAF** in the first place.

There are two types of **URI** s.

One is **Media URI** , which can be obtained using **Media Store API**.

This provides a column named **DATA** that contains an absolute path to a file. This is, in fact, officially deprecated but Google said they will keep this as a read-only field for **File path API**.

So if all the Android developers use **Media Store API** only, what you said is valid.

But there’s another URI called **Document URI** , which can be obtained using **SAF**.

Android developers are now forced to use Google’s native file browser, and **SAF** is the framework that provides the system file picker.

Of course, we can still try implementing our own file browser with **Media Store API** , but there are a lot of limitations and it also ruins the user experience because it needs **READ\_EXTERNAL\_STORAGE** permission to access other app’s media files. (Asking this permission defeats the initial purpose of the new Android’s Scoped Storage.)

**SAF** simplifies all of these and no need for us to ask for any permissions.

But the problem is **Document URI** provides very limited information.

We can get only a file name and file size.

No absolute/relative path at all.

**MediaStore** class has the **getMediaUri()** method but it’s totally useless as said [here](https://stackoverflow.com/q/70029291/4397753).

With **Document Uri** , we can get either an **InputStream** or file descriptor, so if **FMOD\_System\_CreateSound()** supports one of them, it’ll be a big help to us.

Currently, I’m thinking of loading a whole file to RAM and then copying the Java side bytes to the native side.

After that, will pass it to **FMOD\_System\_CreateSound()**.

But this will take a lot of time to load and be wasteful if a user selects an uncompressed large **.wav** file.

So I don’t really want to do this.

Please help with this!

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [December 9, 2021, 6:30pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/5 "2021-12-09T18:30:23Z")

</div>

Adding a file descriptor support seems the best.

As explained [here](https://developer.android.com/training/data-storage/shared/media#update-native-code), we can get a file descriptor integer value via **ParcelFileDescriptor.getFd()** or **detachFd()** , and pass it to **C** ’s **fdopen()**.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [December 11, 2021, 2:02am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/6 "2021-12-11T02:02:50Z")

</div>

Question and answers in [this](https://stackoverflow.com/q/59000390/4397753) thread may be helpful.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [December 12, 2021, 4:54am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/7 "2021-12-12T04:54:32Z")

</div>

**MobileFFmpeg** , which is the older version of **FFmpegKit** , had to go through the same issue last year.

Reading all [these comments](https://github.com/tanersener/mobile-ffmpeg/issues/334) may be helpful too.  
After a long discussion above, one of the contributors added the [_Uri_ support](https://github.com/tanersener/mobile-ffmpeg/pull/440) to the library.

But as I said earlier, File Descriptor support would be more than enough for FMOD.

---

<div class="post-metadata">

**Author:** ![tristanjl](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/tristanjl/32/261_2.png) [@tristanjl](https://qa.fmod.com/u/tristanjl)\
**Post date:** [December 23, 2021, 12:00am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/8 "2021-12-23T00:00:15Z")

</div>

Thanks for the extra information. We will look to make sure the storage access framework is also supported.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [January 1, 2022, 11:59am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/9 "2022-01-01T11:59:25Z")

</div>

Please.  
We will need to migrate to another Audio API otherwise.  
If you think it’ll take too much time, please let us know so that we can explain the situation to our users.

---

<div class="post-metadata">

**Author:** ![tristanjl](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/tristanjl/32/261_2.png) [@tristanjl](https://qa.fmod.com/u/tristanjl)\
**Post date:** [January 10, 2022, 5:59am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/10 "2022-01-10T05:59:16Z")

</div>

It should be in the next release (2.02.06). We will be adding support to createSound for file descriptors on android, which should cover the use case you need.

Edit: support for Uris will be added instead of file descriptors. Still coming in the next release.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [February 8, 2022, 9:15pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/11 "2022-02-08T21:15:43Z")

</div>

It’s the document URI support for **Storage Access Framework** , not Media Store URI, right?

Thank you so much!

CAN’T WAIT!

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [February 14, 2022, 2:56am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/12 "2022-02-14T02:56:30Z")

</div>

Yes, document URI, you pass the `content://` prefixed path into FMOD like a normal file path. Internally we use the file descriptor method to allow file access.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [February 14, 2022, 7:20pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/13 "2022-02-14T19:20:13Z")

</div>

Great! 🙂  
But I think you should check if it works with removable SD cards too.  
I don’t know how you implemented it, but directly accessing “/proc/self/fd/” fails if the file resides in an external SD card.

I don’t know how MobileFFmpeg worked around it, but they successfully added the document Uri support as you can see from the links above.  
I hope it’s also the case with FMOD.

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [February 21, 2022, 11:35pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/14 "2022-02-21T23:35:25Z")

</div>

Thanks, I’ll get QA to do another pass over the feature to make sure this is working.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [May 3, 2022, 6:37pm UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/15 "2022-05-03T18:37:12Z")

</div>

Just checked and found the new version now supports **document Uri** s on Android!  
Thanks a lot for the effort 🙂

I also checked the documentation but couldn’t find any new lines related to the **Uri** s.  
So I assume we can just pass a “content://” style string, which can be obtained by _Uri.toString()_, to _FMOD\_System\_CreateSound()_, just like we did with “file:///android\_asset/” strings.

Is this correct?

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [May 4, 2022, 5:52am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/16 "2022-05-04T05:52:23Z")

</div>

Yes that’s correct.

---

<div class="post-metadata">

**Author:** ![jiyongman](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jiyongman](https://qa.fmod.com/u/jiyongman)\
**Post date:** [May 12, 2022, 9:24am UTC](https://qa.fmod.com/t/scoped-storage-with-mediastore/17332/18 "2022-05-12T09:24:38Z")

</div>

Oops, I deleted by mistake.  
Just tested it and it works like a charm even with files on external SD cards.  
Thank you so so much 🙂
