unity Android 10 获取外部存储所有文件权限

Android 平台需要将文件存储到非沙盒路径下,需要获取MANAGE_EXTERNAL_STORAGE 权限
这个权限需要使用代码获取
        // check skd >= 30 是否有外部存储读写权限 
        public static bool CheckFilePermession()
        {
            AndroidJavaClass buildVersion = new AndroidJavaClass("android.os.Build$VERSION");
            int sdkInt = buildVersion.GetStatic<int>("SDK_INT");

            AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment");
            bool isExternalStorageManager = environment.CallStatic<bool>("isExternalStorageManager");

            if (sdkInt < 30 || isExternalStorageManager)
            {
                Debug.Log("已获得所有权限");
                return true;
            }

            return false;
        }

        // open all file access settings dialogue
        public static void OpenAllFilesAccessSettings()
        {
            try
            {
                // Create a new intent with the action to manage all files access permission
                AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
                intent.Call<AndroidJavaObject>("setAction", "android.settings.MANAGE_ALL_FILES_ACCESS_PERMISSION");

                // Get the current activity and start the intent
                AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                currentActivity.Call("startActivity", intent);
            }
            catch (Exception e)
            {
                Debug.LogError("Failed to open all files access settings: " + e.Message);
            }
        }

 

posted @ 2023-04-27 15:55  liuchong9123  阅读(399)  评论(0编辑  收藏  举报