在内部存储器中创建文件夹?

时间:2018-06-19 15:20:01

标签: c# android unity3d

我尝试在android内部存储器中创建一个文件夹,但是当我统一构建一个应用程序然后在android上运行一个应用程序后,出现异常:

  

“对路径的未经授权的访问例外访问被拒绝”

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;
            }   
    }

1 个答案:

答案 0 :(得分:2)

您应该在Android中启用此权限READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions。 您可以关注此博客以在运行时启用权限,也可以输入清单 https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

或检查

检查并确认是否尚未授予用户 READ_EXTERNAL_STORAGE WRITE_EXTERNAL_STORAGE ,请使用以下代码;

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

请参见references references 2