| | |
| | | using System; |
| | | using Shared; |
| | | using HDL_ON.DAL.Server; |
| | | using System.IO; |
| | | |
| | | namespace HDL_ON |
| | | { |
| | |
| | | return m_Current; |
| | | } |
| | | } |
| | | #endregion |
| | | |
| | | |
| | | #region 文件读写 |
| | | /// <summary> |
| | | /// 根目录 |
| | | /// </summary> |
| | | string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/"; |
| | | /// <summary> |
| | | /// 文件是否存在 |
| | | /// </summary> |
| | | /// <param name="filePath">File path.</param> |
| | | public bool Exists(string filePath) |
| | | { |
| | | return File.Exists(Path.Combine(RootPath, filePath)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// Writes the file by bytes. |
| | | /// </summary> |
| | | /// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns> |
| | | /// <param name="fileName">File name.</param> |
| | | /// <param name="bytes">Bytes.</param> |
| | | public bool WriteFileByBytes(string fileName, byte[] bytes) |
| | | { |
| | | if (fileName == null || (fileName = fileName.Trim()) == ("")) |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | FileStream fs = null; |
| | | |
| | | try |
| | | { |
| | | fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write); |
| | | fs.Write(bytes, 0, bytes.Length); |
| | | fs.Flush(); |
| | | MainPage.Log("SaveFile:" + fileName); |
| | | return true; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | MainPage.Log("FileUtiles Code 113:" + ex.ToString()); |
| | | return false; |
| | | } |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | if (fs != null) |
| | | { |
| | | fs.Close(); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | MainPage.Log("FileUtils Code 121 :" + ex.ToString()); |
| | | } |
| | | } |
| | | } |
| | | public byte[] ReadFile(string fileName) |
| | | { |
| | | FileStream fs = null; |
| | | try |
| | | { |
| | | if (File.Exists(Path.Combine(RootPath, fileName))) |
| | | { |
| | | fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read); |
| | | } |
| | | else if (File.Exists(fileName)) |
| | | { |
| | | fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); |
| | | } |
| | | else |
| | | { |
| | | return new byte[0]; |
| | | } |
| | | byte[] bytes = new byte[fs.Length]; |
| | | fs.Read(bytes, 0, bytes.Length); |
| | | return bytes; |
| | | } |
| | | catch |
| | | { |
| | | return new byte[0]; |
| | | } |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | if (fs != null) |
| | | { |
| | | fs.Close(); |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region 房间、场景 图片上传和下载 |
| | |
| | | else |
| | | { |
| | | //2.自定义图片,检测是否本地缓存过,没有则云端下载然后缓存 |
| | | if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) |
| | | if (!ignoreCache && Exists(imageKey)) |
| | | { |
| | | //2.1 本地有缓存并且不忽略缓存,直接本地读取 |
| | | if (imageView != null) |
| | | { |
| | | imageView.ImageBytes = Common.FileUtlis.Files.ReadFile(imageKey); |
| | | imageView.ImageBytes = ReadFile(imageKey); |
| | | //Utlis.WriteLine("imageKey 加载自定义缓存图片成功"); |
| | | } |
| | | } |
| | |
| | | byte[] imageBytes = GetImageDownloadUrl(imageKey); |
| | | if (imageBytes != null) |
| | | { |
| | | Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); |
| | | WriteFileByBytes(imageKey, imageBytes); |
| | | //2.3 下载成功,显示并缓存本地 |
| | | Application.RunOnMainThread(() => |
| | | { |
| | |
| | | else |
| | | { |
| | | //2.自定义图片,检测是否本地缓存过,没有则云端下载然后缓存 |
| | | if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) |
| | | if (!ignoreCache && Exists(imageKey)) |
| | | { |
| | | //2.1 本地有缓存并且不忽略缓存,直接本地读取 |
| | | if (frameLayout != null) |
| | |
| | | byte[] imageBytes = GetImageDownloadUrl(imageKey); |
| | | if (imageBytes != null) |
| | | { |
| | | Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); |
| | | WriteFileByBytes(imageKey, imageBytes); |
| | | //2.3 下载成功,显示并缓存本地 |
| | | Application.RunOnMainThread(() => |
| | | { |
| | |
| | | { |
| | | var imageKey = GetHeadImageKey(userId); |
| | | byte[] imageBytes; |
| | | if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey)) |
| | | if (!ignoreCache && Exists(imageKey)) |
| | | { |
| | | //本地有缓存 |
| | | imageBytes = Common.FileUtlis.Files.ReadFile(imageKey); |
| | | imageBytes = ReadFile(imageKey); |
| | | } |
| | | else |
| | | { |
| | |
| | | if(imageBytes != null) |
| | | { |
| | | //保存到本地 |
| | | Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes); |
| | | WriteFileByBytes(imageKey, imageBytes); |
| | | } |
| | | |
| | | } |