PoemPhotoDataManager.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.IO;
  3. using ET;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class PoemPhotoDataManager : SingletonBase<PoemPhotoDataManager>
  8. {
  9. /// <summary>
  10. /// 将照片保存到本地
  11. /// </summary>
  12. public void SavePicturoToLocal(Texture2D tex)
  13. {
  14. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  15. //判断目录是否存在,不存在则会创建目录
  16. if (!Directory.Exists(path))
  17. {
  18. try
  19. {
  20. Directory.CreateDirectory(path);
  21. }
  22. catch (Exception exception)
  23. {
  24. throw new Exception("创建文件夹失败, error:" + exception.Message);
  25. }
  26. }
  27. string fileName = "wsj" + TimeHelper.ServerNowSecs + ".jpg";
  28. var filePath = path + fileName;
  29. byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  30. File.WriteAllBytes(filePath, bytes);
  31. UpdateSystemPhoto(filePath);
  32. }
  33. //调用iOS或Android原生方法保存图片后更新相册.
  34. private void UpdateSystemPhoto(string filePath)
  35. {
  36. #if UNITY_ANDROID
  37. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  38. androidJavaObject.Call("scanFile", filePath, "已保存至相册"); //这里我们可以设置保存成功弹窗内容
  39. #endif
  40. }
  41. }
  42. }