From a1ac20d9cbe90b566bffe3ed39a6e07700c3248f Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期一, 19 六月 2023 10:30:06 +0800 Subject: [PATCH] Merge branch 'wjc' --- app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java | 196 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 196 insertions(+), 0 deletions(-) diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java new file mode 100644 index 0000000..958fb8b --- /dev/null +++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlFileLogic.java @@ -0,0 +1,196 @@ +package com.hdl.photovoltaic.other; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.Log; + +import com.hdl.photovoltaic.HDLApp; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * 鏂囦欢鐣岄潰鐨勯�昏緫 + */ +public class HdlFileLogic { + + private static volatile HdlFileLogic sHdlFileLogic; + + /** + * 鑾峰彇褰撳墠瀵硅薄 + * + * @return HdlFileLogic + */ + public static synchronized HdlFileLogic getInstance() { + if (sHdlFileLogic == null) { + synchronized (HdlFileLogic.class) { + if (sHdlFileLogic == null) { + sHdlFileLogic = new HdlFileLogic(); + } + } + + } + return sHdlFileLogic; + } + + private static final String TAG = "FileUtils"; + + public static final String userId = ""; + + /** + * 鑾峰彇鍐呴儴瀛樺偍鏂囦欢璺緞 + */ + private String getInternalStoreFilesPath() { + return HDLApp.getInstance().getFilesDir().getAbsolutePath(); + } + + /** + * 鑾峰彇App瀛樻斁鏂囦欢鐨勬牴璺緞 + */ + public String getAppFilesPath() { + return HDLApp.getInstance().getFilesDir().getAbsolutePath() + userId + "/home"; + } + + public File createFile(String dirPath, String fileName) { + try { + File dirFile = new File(dirPath); + if (!dirFile.exists()) { + if (!createFileDir(dirFile)) { + HdlLogLogic.print(TAG, "createFile dirFile.mkdirs fail"); + return null; + } + } else if (!dirFile.isDirectory()) { + boolean delete = dirFile.delete(); + if (delete) { + return createFile(dirPath, fileName); + } else { + HdlLogLogic.print(TAG, "createFile dirFile !isDirectory and delete fail"); + return null; + } + } + File file = new File(dirPath, fileName); + if (!file.exists()) { + if (!file.createNewFile()) { + Log.e(TAG, "createFile createNewFile fail"); + return null; + } + } + return file; + } catch (Exception e) { + HdlLogLogic.print(TAG, "createFile fail :" + e.getMessage()); + e.printStackTrace(); + return null; + } + } + + /** + * 鍒涘缓鏂囦欢澶�---涔嬫墍浠ヨ涓�灞傚眰鍒涘缓锛屾槸鍥犱负涓�娆℃�у垱寤哄灞傛枃浠跺す鍙兘浼氬け璐ワ紒 + */ + public boolean createFileDir(File dirFile) { + if (dirFile == null) { + return true; + } + if (dirFile.exists()) { + return true; + } + File parentFile = dirFile.getParentFile(); + if (parentFile != null && !parentFile.exists()) { + //鐖舵枃浠跺す涓嶅瓨鍦紝鍒欏厛鍒涘缓鐖舵枃浠跺す锛屽啀鍒涘缓鑷韩鏂囦欢澶� + return createFileDir(parentFile) && createFileDir(dirFile); + } else { + boolean mkdirs = dirFile.mkdirs(); + boolean isSuccess = mkdirs || dirFile.exists(); + if (!isSuccess) { + Log.e("FileUtil", "createFileDir fail " + dirFile); + } + return isSuccess; + } + } + + public void writeFile(String path, String data) { + try { + File file = new File(path); + if (!file.exists()) { + if (!file.mkdir() && !file.isDirectory()) { + HdlLogLogic.print(TAG, "Error: make dir failed!"); + return; + } + } + FileOutputStream d = new FileOutputStream(file); + d.write(data.getBytes()); + d.flush(); + d.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public String readFile(String filepath) { + try { + String fileContent = ""; + if (null == filepath) { + Log.d(TAG, "Error: Invalid file name!"); + return fileContent; + } + + File f = new File(filepath); + if (!f.exists()) { + return fileContent; + } + FileInputStream fis = new FileInputStream(f); + byte[] bytes = new byte[fis.available()]; + fis.read(bytes); + fis.close(); + fileContent = new String(bytes); + return fileContent; + } catch (Exception e1) { + e1.printStackTrace(); + Log.d(TAG, "Error: Input File not find!"); + return ""; + } + + } + + /** + * 鎶婁綅鍥炬暟鎹繚瀛樺埌鎸囧畾璺緞鐨勫浘鐗囨枃浠� + */ + public void writeImage(String path, Bitmap bitmap) { + try { + //鏍规嵁鎸囧畾鏂囦欢璺緞鏋勫缓缂撳瓨杈撳嚭娴佸璞� + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path)); + //鎶婁綅鍥炬暟鎹帇缂╁埌缂撳瓨杈撳嚭娴佷腑 + bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); + //瀹屾垚缂撳瓨杈撳嚭娴佺殑鍐欏叆鍔ㄤ綔 + bos.flush(); + //鍏抽棴缂撳瓨杈撳嚭娴� + bos.close(); + + + } catch (Exception e) { + } + } + + /** + * 浠庢寚瀹氳矾寰勭殑鍥剧墖鏂囦欢涓鍙栦綅鍥炬暟鎹� + */ + public Bitmap readImage(String path) { + try { + //鏍规嵁鎸囧畾鏂囦欢璺緞鏋勫缓缂撳瓨杈撳叆娴佸璞� + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path)); + //浠庣紦瀛樿緭鍏ユ祦涓В鐮佷綅鍥炬暟鎹� + Bitmap bitmap = BitmapFactory.decodeStream(bis); + //鍏抽棴缂撳瓨杈撳叆娴� + bis.close(); + return bitmap; + + + } catch (Exception e) { + } + return null; + } +} -- Gitblit v1.8.0