From 75495ccac9421ddaed2b9d479cbb52e59e4d36cc Mon Sep 17 00:00:00 2001 From: zgf Date: Fri, 28 Jun 2024 10:34:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=BC=93=E5=AD=98=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zgf --- library/src/main/ets/ImageKnifeDispatcher.ets | 2 +- library/src/main/ets/utils/FileCache.ets | 24 ++++++++++++++----- .../src/main/ets/pages/InitImageKnife.ets | 8 +++++-- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/library/src/main/ets/ImageKnifeDispatcher.ets b/library/src/main/ets/ImageKnifeDispatcher.ets index 279706d..13424ed 100644 --- a/library/src/main/ets/ImageKnifeDispatcher.ets +++ b/library/src/main/ets/ImageKnifeDispatcher.ets @@ -175,7 +175,7 @@ export class ImageKnifeDispatcher { requestSource: requestSource, isWatchProgress: isWatchProgress, memoryKey: memoryKey, - fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder() + fileCacheFolder: ImageKnife.getInstance().getFileCache()?.getCacheFolder() } diff --git a/library/src/main/ets/utils/FileCache.ets b/library/src/main/ets/utils/FileCache.ets index a9beaa4..4aec883 100644 --- a/library/src/main/ets/utils/FileCache.ets +++ b/library/src/main/ets/utils/FileCache.ets @@ -60,13 +60,17 @@ export class FileCache { if (this.isInited) { return } - if (this.context && path.startsWith(this.context.cacheDir) === true) { - this.path = path + if( path.includes("/") ) { + if ( await FileUtils.getInstance().createFolder(path+"/")) { + throw Error("Failed to create the directory") + } else { + this.path = path + } } else { FileCache.CACHE_FOLDER = path this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR + await FileUtils.getInstance().createFolder(this.path) } - await FileUtils.getInstance().createFolder(this.path) // 遍历缓存目录下的文件,按照时间顺序加入缓存 let filenames: string[] = await FileUtils.getInstance().ListFile(this.path) @@ -112,7 +116,11 @@ export class FileCache { } public getCacheFolder(): string { - return FileCache.CACHE_FOLDER + if(this.context && this.path.startsWith(this.context.cacheDir) == true) { + return FileCache.CACHE_FOLDER + } else { + return this.path + } } // 添加缓存键值对,同时写文件 @@ -254,9 +262,11 @@ export class FileCache { * @param value */ static saveFileCacheOnlyFile(context: Context, key: string, value: ArrayBuffer, folder: string = FileCache.CACHE_FOLDER): boolean { + let path = context.cacheDir + FileUtils.SEPARATOR + folder + path = folder.includes("/") ? folder : path // 写文件 FileUtils.getInstance() - .writeFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key, value) + .writeFileSync(path + FileUtils.SEPARATOR + key, value) return true } @@ -267,9 +277,11 @@ export class FileCache { * @returns */ static getFileCacheByFile(context: Context, key: string, folder: string = FileCache.CACHE_FOLDER): ArrayBuffer | undefined { + let path = context.cacheDir + FileUtils.SEPARATOR + folder + path = folder.includes("/") ? folder : path // 从文件获取查看是否有缓存 return FileUtils.getInstance() - .readFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key) + .readFileSync(path + FileUtils.SEPARATOR + key) } /** diff --git a/sharedlibrary/src/main/ets/pages/InitImageKnife.ets b/sharedlibrary/src/main/ets/pages/InitImageKnife.ets index 7bf492c..e2dbd09 100644 --- a/sharedlibrary/src/main/ets/pages/InitImageKnife.ets +++ b/sharedlibrary/src/main/ets/pages/InitImageKnife.ets @@ -15,7 +15,11 @@ import { ImageKnife } from '@ohos/imageknife' import common from '@ohos.app.ability.common' export class InitImageKnife{ - static async init(entryContext:common.UIAbilityContext){ - await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache") + static async init(entryContext:common.UIAbilityContext,flag: boolean = true){ + if ( flag ) { + await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache") + } else { + await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"/data/storage/el2/100/hap/files/ImageKnife") + } } } \ No newline at end of file