# UnauthorizedAccessException in ExternalStorage

**URL:** <https://forum.magicleap.cloud/t/unauthorizedaccessexception-in-externalstorage/5789>\
**Category:** Unity Development\
**Created:** [May 28, 2025, 5:52pm UTC](https://forum.magicleap.cloud/t/unauthorizedaccessexception-in-externalstorage/5789 "2025-05-28T17:52:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oleksii.kryzhanovskyi](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@oleksii.kryzhanovskyi](https://forum.magicleap.cloud/u/oleksii.kryzhanovskyi)\
**Post date:** [May 28, 2025, 5:52pm UTC](https://forum.magicleap.cloud/t/unauthorizedaccessexception-in-externalstorage/5789/1 "2025-05-28T17:52:28Z")

</div>

Hello, I hope you’re doing well.

I have manually checked my permissions via script and they all appear to be correct. I have also added them to the manifest. However, I am still receiving an UnauthorizedAccessException. Is it possible that the Magic Leap 2 system is somehow blocking access to all folders outside the application folder?

**Unity Editor version** : 2022.3.54.f1  
**ML2 OS version** : 1.12.0  
**Unity SDK version** : 2.6.0  
**Host OS** : (Windows/MacOS) Windows 11

**FOR ANDROID**

```auto
System.Collections.IEnumerator RequestAndroidPermissions()
    {
        Debug.Log("--- Requesting Android Permissions ---");
        
        // Check READ_EXTERNAL_STORAGE
        if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
        {
            Debug.Log("Requesting Android READ_EXTERNAL_STORAGE permission...");
            Permission.RequestUserPermission(Permission.ExternalStorageRead);
            
            // Wait for permission dialog
            yield return new WaitForSeconds(1f);
            
            // Check again
            if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
            {
                Debug.Log("✅ Android READ_EXTERNAL_STORAGE granted");
            }
            else
            {
                Debug.LogWarning("⚠️ Android READ_EXTERNAL_STORAGE denied");
            }
        }
        else
        {
            Debug.Log("✅ Android READ_EXTERNAL_STORAGE already granted");
        }
        
        // Check WRITE_EXTERNAL_STORAGE
        if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
        {
            Debug.Log("Requesting Android WRITE_EXTERNAL_STORAGE permission...");
            Permission.RequestUserPermission(Permission.ExternalStorageWrite);
            
            // Wait for permission dialog
            yield return new WaitForSeconds(1f);
            
            if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
            {
                Debug.Log("✅ Android WRITE_EXTERNAL_STORAGE granted");
            }
            else
            {
                Debug.LogWarning("⚠️ Android WRITE_EXTERNAL_STORAGE denied");
            }
        }
        else
        {
            Debug.Log("✅ Android WRITE_EXTERNAL_STORAGE already granted");
        }
        
        androidPermissionsGranted = Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead) ||
                                   Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite);
    }

```

**FOR MAGIC LEAP 2**

```auto
System.Collections.IEnumerator RequestMLPermissions()
            {
                Debug.Log("--- Requesting Magic Leap Permissions ---");

                MLResult resultRead = MLPermissions.CheckPermission(MLPermission.ReadExternalStorage);

                if (resultRead != MLResult.Code.Ok)
                {
                    Debug.Log("Requesting ML ReadExternalStorage permission...");
                    MLPermissions.RequestPermission(MLPermission.ReadExternalStorage, OnMLPermissionResult);

                    // Wait for permission result
                    float timeout = 10f;
                    while (!mlPermissionsGranted && timeout > 0)
                    {
                        timeout -= Time.deltaTime;
                        yield return null;
                    }

                    if (timeout <= 0)
                    {
                        Debug.LogWarning("ML permission request timed out");
                    }
                }
                else
                {
                    Debug.Log("✅ ML ReadExternalStorage permission already granted");
                    mlPermissionsGranted = true;
                }
                
                MLResult resultWrite = MLPermissions.CheckPermission(MLPermission.WriteExternalStorage);

                if (resultWrite != MLResult.Code.Ok)
                {
                    Debug.Log("Requesting ML ReadExternalStorage permission...");
                    MLPermissions.RequestPermission(MLPermission.WriteExternalStorage, OnMLPermissionResult);

                    // Wait for permission result
                    float timeout = 10f;
                    while (!mlPermissionsGranted && timeout > 0)
                    {
                        timeout -= Time.deltaTime;
                        yield return null;
                    }

                    if (timeout <= 0)
                    {
                        Debug.LogWarning("ML permission request timed out");
                    }
                }
                else
                {
                    Debug.Log("✅ ML WriteExternalStorage permission already granted");
                    mlPermissionsGranted = true;
                }
            }

```

I have also added these lines to both the AndroidManifest and ML2.AndroidManifest files.

```auto
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
  <uses-permission android:name="com.magicleap.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="com.magicleap.permission.WRITE_EXTERNAL_STORAGE" />

```

All of my permissions appear to be configured correctly. However, when I attempt to create a file outside the application folder, I receive this error: " Error Unity IOException: Permission denied UnauthorizedAccessException: Access to the path ‘/storage/emulated/0/Download/\_cache’ is denied."

**LOGS**

```auto
2025/05/28 18:39:41.240 24400 24422 Info Unity === REQUESTING PERMISSIONS FOR MAGIC LEAP 2 ===
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.240 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.<Bootstrapper>d__26:MoveNext()
2025/05/28 18:39:41.240 24400 24422 Info Unity System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(TStateMachine&)
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Bootstrapper()
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Start()
2025/05/28 18:39:41.240 24400 24422 Info Unity 
2025/05/28 18:39:41.240 24400 24422 Info Unity --- Requesting Magic Leap Permissions ---
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.<RequestMLPermissions>d__18:MoveNext()
2025/05/28 18:39:41.240 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.240 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.<Bootstrapper>d__26:MoveNext()
2025/05/28 18:39:41.240 24400 24422 Info Unity System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(TStateMachine&)
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Bootstrapper()
2025/05/28 18:39:41.240 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Start()
2025/05/28 18:39:41.240 24400 24422 Info Unity 
2025/05/28 18:39:41.245 24400 24422 Info Unity ✅ ML ReadExternalStorage permission already granted
2025/05/28 18:39:41.245 24400 24422 Info Unity TEST.TEST_App.<RequestMLPermissions>d__18:MoveNext()
2025/05/28 18:39:41.245 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.245 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.245 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.245 24400 24422 Info Unity TEST.TEST_App.<Bootstrapper>d__26:MoveNext()
2025/05/28 18:39:41.245 24400 24422 Info Unity System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(TStateMachine&)
2025/05/28 18:39:41.245 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Bootstrapper()
2025/05/28 18:39:41.245 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Start()
2025/05/28 18:39:41.245 24400 24422 Info Unity 
2025/05/28 18:39:41.246 24400 24422 Info Unity ✅ ML WriteExternalStorage permission already granted
2025/05/28 18:39:41.246 24400 24422 Info Unity TEST.TEST_App.<RequestMLPermissions>d__18:MoveNext()
2025/05/28 18:39:41.246 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.246 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.246 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.246 24400 24422 Info Unity TEST.TEST_App.<Bootstrapper>d__26:MoveNext()
2025/05/28 18:39:41.246 24400 24422 Info Unity System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(TStateMachine&)
2025/05/28 18:39:41.246 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Bootstrapper()
2025/05/28 18:39:41.246 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:Start()
2025/05/28 18:39:41.270 24400 24422 Warn Unity 
2025/05/28 18:39:41.276 24400 24422 Info Unity --- Requesting Android Permissions ---
2025/05/28 18:39:41.276 24400 24422 Info Unity TEST.TEST_App.<RequestAndroidPermissions>d__25:MoveNext()
2025/05/28 18:39:41.276 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.276 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.276 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.276 24400 24422 Info Unity 
2025/05/28 18:39:41.277 24400 24422 Info Unity ✅ Android READ_EXTERNAL_STORAGE already granted
2025/05/28 18:39:41.277 24400 24422 Info Unity TEST.TEST_App.<RequestAndroidPermissions>d__25:MoveNext()
2025/05/28 18:39:41.277 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.277 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.277 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.277 24400 24422 Info Unity 
2025/05/28 18:39:41.278 24400 24422 Info Unity ✅ Android WRITE_EXTERNAL_STORAGE already granted
2025/05/28 18:39:41.278 24400 24422 Info Unity TEST.TEST_App.<RequestAndroidPermissions>d__25:MoveNext()
2025/05/28 18:39:41.278 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.278 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.278 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.292 24400 24422 Info Unity 
2025/05/28 18:39:41.293 24400 24422 Info Unity === FINAL PERMISSION STATUS ===
2025/05/28 18:39:41.293 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:CheckPermissionStatus()
2025/05/28 18:39:41.293 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.293 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.293 24400 24422 Info Unity 
2025/05/28 18:39:41.294 24400 24422 Info Unity ML Permissions: True
2025/05/28 18:39:41.294 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:CheckPermissionStatus()
2025/05/28 18:39:41.294 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.294 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.294 24400 24422 Info Unity 
2025/05/28 18:39:41.294 24400 24422 Info Unity Android Permissions: True
2025/05/28 18:39:41.294 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:CheckPermissionStatus()
2025/05/28 18:39:41.294 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.294 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.294 24400 24422 Info Unity 
2025/05/28 18:39:41.295 24400 24422 Info Unity Can Access Documents: True
2025/05/28 18:39:41.295 24400 24422 Info Unity TEST.TEST_App.BootstrapperV1:CheckPermissionStatus()
2025/05/28 18:39:41.295 24400 24422 Info Unity TEST.TEST_App.<RequestAllPermissions>d__17:MoveNext()
2025/05/28 18:39:41.295 24400 24422 Info Unity UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
2025/05/28 18:39:41.295 24400 24422 Info Unity 
2025/05/28 18:39:41.763 24400 24422 Error Unity IOException: Permission denied
2025/05/28 18:39:41.763 24400 24422 Error Unity Rethrow as UnauthorizedAccessException: Access to the path '/storage/emulated/0/Download/_cache' is denied.
2025/05/28 18:39:41.763 24400 24422 Error Unity at System.IO.FileSystem.CreateDirectory (System.String fullPath) [0x00000] in <00000000000000000000000000000000>:0 
2025/05/28 18:39:41.763 24400 24422 Error Unity at System.IO.Directory.CreateDirectory (System.String path) [0x00000] in <00000000000000000000000000000000>:0 
2025/05/28 18:39:41.763 24400 24422 Error Unity at TEST.Core.Utils.FileSystem.AndroidFileSystemOps.CreateAndOpenSubfolderInCurrentFolderAsync (System.String subfolder) [0x00000] in <00000000000000000000000000000000>:0 
2025/05/28 18:39:41.763 24400 24422 Error Unity at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x00000] in <00000000000000000000000000000000>:0 
2025/05/28 18:39:41.763 24400 24422 Error Unity at TEST.Core.Utils.FileSystem.AndroidFileSystemOps.CreateAndOpenSubfolderInCurrentFolderAsync (System.String subfolder) [0x00000] in <00000000000000000000000000000000>:0 
2025/05/28 18:39:41.763 24400 24422 Error Unity at TEST.Platform.TestCache.TestCacheController.Init () [0x00000] in <00000000000000000000000000000000>:0 
2025/05/28 18:39:41.763 24400 24422 Error Unity at System.Runtime.CompilerServ

```

Thank you for your time and assistance.

---

<div class="post-metadata">

**Author:** ![magicbot](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.magicleap.cloud/magicbot/32/2470_2.png) [@magicbot](https://forum.magicleap.cloud/u/magicbot)\
**Post date:** [May 28, 2025, 6:05pm UTC](https://forum.magicleap.cloud/t/unauthorizedaccessexception-in-externalstorage/5789/2 "2025-05-28T18:05:32Z")

</div>

Here is a post that might be related:

> [@Access denied writing files on USB](https://forum.magicleap.cloud/t/access-denied-writing-files-on-usb/5331/2):
>
> It sounds like you’re running into Android’s scoped storage restrictions, which got stricter starting from Android 10. Even if your USB drive mounts under /storage/XXXX-XXXX, Android often treats it as external storage, meaning standard permissions alone (READ\_EXTERNAL\_STORAGE and WRITE\_EXTERNAL\_STORAGE) aren’t enough. I’d recommend looking at this repo [UnitySimpleFileBrowser](https://github.com/yasirkula/UnitySimpleFileBrowser) —it uses Storage Access Framework which should resolve the ‘access denied’ error you’re facing.

* * *

I recommend looking at these open source projects to see how to read and write files outside of your app’s local directory.

> **[GitHub - yasirkula/UnityNativeFilePicker: A native Unity plugin to import/export files...](https://github.com/yasirkula/UnityNativeFilePicker)**
>
> A native Unity plugin to import/export files from/to various document providers on Android & iOS

> **[GitHub - yasirkula/UnitySimpleFileBrowser: A uGUI based runtime file browser for Unity 3D...](https://github.com/yasirkula/UnitySimpleFileBrowser)**
>
> A uGUI based runtime file browser for Unity 3D (draggable and resizable)
