QianZiItem.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using Newtonsoft.Json.Linq;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using static DownLoadXRManager;
  9. using System.IO;
  10. using UnityEngine.Android;
  11. #if UNITY_ANDROID || UNITY_IOS
  12. using NativeFilePickerNamespace;
  13. #endif
  14. public class QianZiItem : MonoBehaviour
  15. {
  16. public string scpath;
  17. public Texture2D defaultTexture;
  18. public void showqianzi()
  19. {
  20. if(downloadtex!=null)
  21. {
  22. Destroy(downloadtex);
  23. }
  24. AdvancedDrawingPad.callback = (tex)=>{
  25. byte[] pngdata=tex.EncodeToPNG();
  26. int fileSize = pngdata.Length;
  27. string uuid = Guid.NewGuid().ToString();
  28. string fileName = uuid+".png";
  29. string objecName = "workorder/"+int.Parse(ChuLiItemManager.ChooseData["id"].ToString())+"/";
  30. scpath = objecName +fileName;
  31. StartCoroutine(SaveToGalleryAndGetPath(pngdata,fileName,(filePath)=>{
  32. // 文件预上传
  33. JsonData jData = new JsonData();
  34. jData["size"] = fileSize;
  35. jData["ext"] = "jpg";
  36. jData["name"] = fileName;
  37. JsonData fileData = new JsonData();
  38. fileData["filePath"] = filePath;
  39. fileData["fileSize"] = fileSize;
  40. fileData["fileName"] = fileName;
  41. // project /{ projectId}/ inspection /{ projectInspectionId}/{ taskId}
  42. fileData["objectName"] = objecName;
  43. JsonData fildData = fileData;
  44. // 文件上传
  45. DownLoadXRManager.PutFile(fildData["filePath"].ToString(), fildData["fileName"].ToString(), fildData["objectName"].ToString() + fildData["fileName"].ToString(),uuid, int.Parse(fildData["fileSize"].ToString()), msg =>
  46. {
  47. Debug.Log(" 文件上传成功 " + msg);
  48. });
  49. // 使用方式
  50. // string internalPath = CopyToInternalStorage(filePath);
  51. // OpenFile(internalPath);
  52. // "workorder/1/352b97c2-a9ea-49e8-bb6b-39e9a02f7ee5.png";
  53. })) ;
  54. downloadtex = tex;
  55. this.transform.GetComponent<RawImage>().texture = tex;
  56. };
  57. TianJiWindowManager.Instance.showPop(3);
  58. }private string CopyToInternalStorage(string originalPath)
  59. {
  60. string internalPath = Path.Combine(Application.persistentDataPath, Path.GetFileName(originalPath));
  61. File.Copy(originalPath, internalPath, true);
  62. return internalPath;
  63. }
  64. public void OpenFile(string filePath)
  65. {
  66. if (!File.Exists(filePath))
  67. {
  68. Debug.LogError("文件不存在: " + filePath);
  69. return;
  70. }
  71. #if UNITY_ANDROID && !UNITY_EDITOR
  72. StartCoroutine(TryOpenFileAndroid(filePath));
  73. #endif
  74. }
  75. private IEnumerator TryOpenFileAndroid(string filePath)
  76. {
  77. yield return null;
  78. try
  79. {
  80. AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
  81. AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
  82. AndroidJavaObject currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
  83. .GetStatic<AndroidJavaObject>("currentActivity");
  84. // 使用 FileProvider
  85. AndroidJavaClass fileProvider = new AndroidJavaClass("androidx.core.content.FileProvider");
  86. AndroidJavaObject fileObj = new AndroidJavaObject("java.io.File", filePath);
  87. string authority = currentActivity.Call<string>("getPackageName") + ".fileprovider";
  88. AndroidJavaObject uri = fileProvider.CallStatic<AndroidJavaObject>(
  89. "getUriForFile",
  90. currentActivity,
  91. authority,
  92. fileObj);
  93. // 创建 Intent
  94. AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
  95. intent.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_VIEW"));
  96. intent.Call<AndroidJavaObject>("setDataAndType", uri, GetMimeType(filePath));
  97. intent.Call<AndroidJavaObject>("addFlags",
  98. intentClass.GetStatic<int>("FLAG_GRANT_READ_URI_PERMISSION"));
  99. // 检查是否有应用可以处理
  100. AndroidJavaObject pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
  101. bool canOpen = intent.Call<AndroidJavaObject>("resolveActivity", pm) != null;
  102. if (canOpen)
  103. {
  104. currentActivity.Call("startActivity", intent);
  105. }
  106. else
  107. {
  108. Debug.LogError("没有应用可以打开此文件类型");
  109. }
  110. }
  111. catch (System.Exception e)
  112. {
  113. Debug.LogError("打开文件失败: " + e.Message);
  114. }
  115. }
  116. private string GetMimeType(string filePath)
  117. {
  118. string extension = Path.GetExtension(filePath).ToLower();
  119. switch (extension)
  120. {
  121. case ".pdf": return "application/pdf";
  122. case ".doc": case ".docx": return "application/msword";
  123. case ".xls": case ".xlsx": return "application/vnd.ms-excel";
  124. case ".jpg": case ".jpeg": return "image/jpeg";
  125. case ".png": return "image/png";
  126. case ".mp4": return "video/mp4";
  127. case ".avi": return "video/x-msvideo";
  128. default: return "*/*";
  129. }
  130. }
  131. public IEnumerator SaveToGalleryAndGetPath(byte[] imageData, string filename, System.Action<string> callback)
  132. {
  133. string path = null;
  134. bool done = false;
  135. if (Application.platform == RuntimePlatform.Android ||
  136. Application.platform == RuntimePlatform.IPhonePlayer)
  137. {
  138. NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(
  139. imageData,
  140. "MyAppPhotos", // 相册名称
  141. filename,
  142. (success, savedPath) =>
  143. {
  144. path = success ? savedPath : null;
  145. done = true;
  146. });
  147. Debug.Log("Permission status: " + permission);
  148. yield return new WaitUntil(() => done);
  149. }
  150. else
  151. {
  152. // 非移动平台,保存到本地
  153. string localPath = System.IO.Path.Combine(Application.persistentDataPath, filename);
  154. System.IO.File.WriteAllBytes(localPath, imageData);
  155. path = localPath;
  156. }
  157. callback?.Invoke(path);
  158. }
  159. Texture2D downloadtex;
  160. public void downloadimg(string path)
  161. {
  162. if(path==""||path==null)
  163. {
  164. return;
  165. }
  166. if(downloadtex!=null)
  167. {
  168. Destroy(downloadtex);
  169. }
  170. Debug.Log("开始下载图片===》"+path);
  171. DownLoadResources dlr = new DownLoadResources();
  172. dlr.path = path;
  173. // 文件上传
  174. DownLoadXRManager.DownLoadForTexture(dlr, msg =>
  175. {
  176. downloadtex=msg;
  177. this.transform.GetComponent<RawImage>().texture = msg;
  178. },(f)=>{
  179. });
  180. }
  181. }