From 1ecba3f7432912d4eb7637355f4dfae676059e00 Mon Sep 17 00:00:00 2001 From: wxr <464027401@qq.com> Date: 星期二, 15 十二月 2020 17:23:58 +0800 Subject: [PATCH] 1 --- HDL_ON/Common/ImageUtlis.cs | 139 +++++++++++++++++++++++++++++++++++++++------- 1 files changed, 118 insertions(+), 21 deletions(-) diff --git a/HDL_ON/Common/ImageUtlis.cs b/HDL_ON/Common/ImageUtlis.cs index 88f00e1..83e55bd 100644 --- a/HDL_ON/Common/ImageUtlis.cs +++ b/HDL_ON/Common/ImageUtlis.cs @@ -1,11 +1,7 @@ 锘縰sing System; using Shared; -using HDL_ON; -using HDL_ON.UI.CSS; using HDL_ON.DAL.Server; -using HDL_ON.UI; -using System.Threading; -using HDL_ON.Entity; +using System.IO; namespace HDL_ON { @@ -39,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 鎴块棿銆佸満鏅� 鍥剧墖涓婁紶鍜屼笅杞� @@ -106,19 +203,19 @@ { imageView.ImageBytes = null; imageView.ImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); } } else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && FileUtils.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (imageView != null) { - imageView.ImageBytes = FileUtils.ReadFile(imageKey); - Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); + imageView.ImageBytes = ReadFile(imageKey); + //Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); } } else @@ -129,21 +226,21 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - FileUtils.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { if (imageView != null) { imageView.ImageBytes = imageBytes; - Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); } }); } else { //2.4 涓嬭浇鏄惁锛屾槸鍚︿娇鐢ㄩ粯璁ゅ浘鐗� - Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); } }); } @@ -177,19 +274,19 @@ { //imageView.ImageBytes = null; frameLayout.BackgroundImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇鏈湴鍥剧墖鎴愬姛"); } } else { //2.鑷畾涔夊浘鐗囷紝妫�娴嬫槸鍚︽湰鍦扮紦瀛樿繃锛屾病鏈夊垯浜戠涓嬭浇鐒跺悗缂撳瓨 - if (!ignoreCache && FileUtils.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //2.1 鏈湴鏈夌紦瀛樺苟涓斾笉蹇界暐缂撳瓨锛岀洿鎺ユ湰鍦拌鍙� if (frameLayout != null) { frameLayout.BackgroundImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); + //Utlis.WriteLine("imageKey 鍔犺浇鑷畾涔夌紦瀛樺浘鐗囨垚鍔�"); } } else @@ -200,21 +297,21 @@ byte[] imageBytes = GetImageDownloadUrl(imageKey); if (imageBytes != null) { - FileUtils.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); //2.3 涓嬭浇鎴愬姛锛屾樉绀哄苟缂撳瓨鏈湴 Application.RunOnMainThread(() => { if (frameLayout != null) { frameLayout.BackgroundImagePath = imageKey; - Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠涓嬭浇鍥剧墖鎴愬姛"); } }); } else { //2.4 涓嬭浇鏄惁锛屾槸鍚︿娇鐢ㄩ粯璁ゅ浘鐗� - Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); + //Utlis.WriteLine("imageKey 鍔犺浇浜戠鍥剧墖澶辫触"); } }); } @@ -264,10 +361,10 @@ { var imageKey = GetHeadImageKey(userId); byte[] imageBytes; - if (!ignoreCache && FileUtils.Exists(imageKey)) + if (!ignoreCache && Exists(imageKey)) { //鏈湴鏈夌紦瀛� - imageBytes = FileUtils.ReadFile(imageKey); + imageBytes = ReadFile(imageKey); } else { @@ -276,7 +373,7 @@ if(imageBytes != null) { //淇濆瓨鍒版湰鍦� - FileUtils.WriteFileByBytes(imageKey, imageBytes); + WriteFileByBytes(imageKey, imageBytes); } } -- Gitblit v1.8.0