JLChen
2020-12-14 15978205aa7e4aeca8af3d3d34a80a85f0265f63
HDL_ON/Common/ImageUtlis.cs
@@ -1,6 +1,7 @@
using System;
using Shared;
using HDL_ON.DAL.Server;
using System.IO;
namespace HDL_ON
{
@@ -34,6 +35,107 @@
                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 房间、场景 图片上传和下载
@@ -107,12 +209,12 @@
                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 加载自定义缓存图片成功");
                        }
                    }
@@ -124,7 +226,7 @@
                            byte[] imageBytes = GetImageDownloadUrl(imageKey);
                            if (imageBytes != null)
                            {
                                Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes);
                                WriteFileByBytes(imageKey, imageBytes);
                                //2.3 下载成功,显示并缓存本地
                                Application.RunOnMainThread(() =>
                                {
@@ -178,7 +280,7 @@
                else
                {
                    //2.自定义图片,检测是否本地缓存过,没有则云端下载然后缓存
                    if (!ignoreCache && Common.FileUtlis.Files.Exists(imageKey))
                    if (!ignoreCache && Exists(imageKey))
                    {
                        //2.1 本地有缓存并且不忽略缓存,直接本地读取
                        if (frameLayout != null)
@@ -195,7 +297,7 @@
                            byte[] imageBytes = GetImageDownloadUrl(imageKey);
                            if (imageBytes != null)
                            {
                                Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes);
                                WriteFileByBytes(imageKey, imageBytes);
                                //2.3 下载成功,显示并缓存本地
                                Application.RunOnMainThread(() =>
                                {
@@ -259,10 +361,10 @@
            {
                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
                {
@@ -271,7 +373,7 @@
                    if(imageBytes != null)
                    {
                        //保存到本地
                        Common.FileUtlis.Files.WriteFileByBytes(imageKey, imageBytes);
                        WriteFileByBytes(imageKey, imageBytes);
                    }
                   
                }