2

I try to create a folder in the internal memory android, but after I build an app in unity then run an app on android, I get the exception:

"unauthorizedaccessexception access to the path is denied"

void Start()
    {
        //Create cashe  
        AddCacheAtPath("cashe");
    }
    private void AddCacheAtPath(string path)
    {
         if (!Directory.Exists(path))
              Directory.CreateDirectory(path);

           Cache newCache = Caching.AddCache(path);

            //Make sure your new Cache is valid
            if (newCache.valid)
            {
                //If you wanted to set your newly created cache to the active cache
                Caching.currentCacheForWriting = newCache;
            }   
    }
MohammadTofi
  • 365
  • 1
  • 7
  • 22

1 Answers1

2

You should enable this permission READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions in Android. You can follow this blog to enable the permission in runtime or you can give in manifest https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

Or Check

Check and if user has not yet granted granted READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE, use the bellow code;

var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
            RequestPermissions(permissions, 77);

See references references 2

Hitesh Anshani
  • 1,499
  • 9
  • 19
  • A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Sebastian Hofmann Jun 19 '18 at 15:23
  • Thanks For reply, but why I need to enable EXTERNAL_STORAGE permission if I want to create a folder in internal memory. – MohammadTofi Jun 19 '18 at 15:27
  • good question but some users don't want to make a folder in our system that's why android applications need permissions @MohammadTofi – Hitesh Anshani Jun 19 '18 at 15:29
  • See this https://forums.xamarin.com/discussion/105524/xamarin-android-system-unauthorizedaccessexception-access-to-the-path-is-denied – Hitesh Anshani Jun 19 '18 at 15:32
  • I create my app in unity, so in the player setting there is an option said "write permeation"--> "internal card" I select this option, but I still have the problem. – MohammadTofi Jun 19 '18 at 15:37
  • But Application also needs permissions can include permissions in manifest file of project – Hitesh Anshani Jun 19 '18 at 15:40