HarmonyOS 清理app缓存的方法

HarmonyOS
2024-12-23 16:22:47
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

当前没有清除缓存的接口,推荐下面:查询缓存用storageStatistics.getCurrentBundleStats()接口。

清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存。

详细用法见下面的链接:

查询:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5

import { common } from '@kit.AbilityKit'
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';

function clearCache(context:common.UIAbilityContext) {
  let cacheDir = context.cacheDir
  fs.listFile(cacheDir).then((filenames) => {
    for (let i = 0;i < filenames.length; i++) {
      let dirPath = cacheDir + filenames[i] // 判断是否文件夹
      let isDirectory: boolean = false
      try {
        isDirectory = fs.statSync(dirPath).isDirectory()
      } catch (e) {
        console.log(e)
      }
      if (isDirectory) {
        fs.rmdirSync(dirPath)
      } else {
        fs.unlink(dirPath).then(() => {
          console.info('remove file succeed');
        }).catch((err: BusinessError) => {
          console.info("remove file failed with error message: " + err.message + ", error code: " + err.code);
        });
      }
    }
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
分享
微博
QQ
微信
回复
2024-12-23 18:36:43
相关问题
HarmonyOS 清理缓存问题
659浏览 • 1回复 待解决
HarmonyOS web清理缓存
906浏览 • 1回复 待解决
HarmonyOS web 清理缓存
599浏览 • 1回复 待解决
HarmonyOS 如何清理web缓存
445浏览 • 1回复 待解决
HarmonyOS ArkWeb如何清理缓存
530浏览 • 1回复 待解决
HarmonyOS 关于应用清理缓存
1162浏览 • 1回复 待解决
HarmonyOS 缓存无法清理干净
863浏览 • 1回复 待解决
HarmonyOS 如何清理 web 缓存
570浏览 • 1回复 待解决
HarmonyOS 如何清理Image磁盘缓存
850浏览 • 1回复 待解决
如何获取缓存数据和清理缓存
1255浏览 • 1回复 待解决
HarmonyOS 应用缓存清理不彻底
654浏览 • 1回复 待解决
HarmonyOS 如何获取应用缓存清理
618浏览 • 1回复 待解决
用file api清理缓存目录
1380浏览 • 1回复 待解决
DevEco有没有清理调试应用缓存功能
5034浏览 • 1回复 待解决
HarmonyOS 是否有清理urlcache方法
1089浏览 • 1回复 待解决
HarmonyOS 清除APP缓存问题
577浏览 • 1回复 待解决
HarmonyOS 清除app缓存
711浏览 • 1回复 待解决