zhaoyang 3 years ago
parent
commit
2aed94826a
1 changed files with 19 additions and 15 deletions
  1. 19 15
      GameClient/Assets/Game/HotUpdate/Views/DressUp/PhotographSaveView.cs

+ 19 - 15
GameClient/Assets/Game/HotUpdate/Views/DressUp/PhotographSaveView.cs

@@ -40,8 +40,9 @@ namespace GFGGame
 
 
 
 
 #if UNITY_ANDROID
 #if UNITY_ANDROID
+            int index = Application.persistentDataPath.IndexOf("Android");
+            path = index < 0 ? path : Application.persistentDataPath.Substring(0, index);
 
 
-            path = Application.persistentDataPath.IndexOf("Android") > 0 ? path.Substring(0, Application.persistentDataPath.IndexOf("Android")) + "Pictures/万世镜" : path + "/Pictures/万世镜";
 #endif
 #endif
             //判断目录是否存在,不存在则会创建目录
             //判断目录是否存在,不存在则会创建目录
             if (!Directory.Exists(path))
             if (!Directory.Exists(path))
@@ -57,25 +58,16 @@ namespace GFGGame
                 }
                 }
             }
             }
             string fileName = DateUtils.Instance.GetCurTime() + ".png";
             string fileName = DateUtils.Instance.GetCurTime() + ".png";
-            path = path + "/" + fileName;
+
 
 
             Debug.Log("文件路径:" + path);
             Debug.Log("文件路径:" + path);
             Texture2D tex = this.viewData as Texture2D;
             Texture2D tex = this.viewData as Texture2D;
             byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
             byte[] bytes = tex.EncodeToPNG();//将纹理数据,转化成一个png图片
-            try
-            {
-                FileStream newFs = new FileStream(path, FileMode.Create, FileAccess.Write);
-                newFs.Write(bytes, 0, bytes.Length);
-                newFs.Close();
-                newFs.Dispose();
-            }
-            catch (Exception ex)
-            {
-                throw new Exception("保存失败, error:" + ex.Message);
-            }
-            // string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
-            // File.WriteAllBytes(path, bytes);
+            File.WriteAllBytes(path + "/Pictures/Screenshots/" + fileName, bytes);
             Timers.inst.StartCoroutine(GetSaveState(path));// ();
             Timers.inst.StartCoroutine(GetSaveState(path));// ();
+            string[] paths = new string[1];
+            paths[0] = path;
+            ScanFile(paths);
         }
         }
         private IEnumerator GetSaveState(string path)
         private IEnumerator GetSaveState(string path)
         {
         {
@@ -85,6 +77,18 @@ namespace GFGGame
                 PromptController.Instance.ShowFloatTextPrompt("保存成功");
                 PromptController.Instance.ShowFloatTextPrompt("保存成功");
             }
             }
         }
         }
+        //刷新图片,显示到相册中
+        void ScanFile(string[] path)
+        {
+            using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
+            {
+                AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
+                using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
+                {
+                    Conn.CallStatic("scanFile", playerActivity, path, null, null);
+                }
+            }
+        }
         protected override void OnHide()
         protected override void OnHide()
         {
         {
             base.OnHide();
             base.OnHide();