鸿蒙开发5.0【APP占用空间管理】

使用接口

接口 接口能力
storageStatistics.getCurrentBundleStats 获取当前应用的存储空间大小 (包含缓存文件,安装文件等)
statvfs.getFreeSize 查询设备剩余可用空间
statvfs.getTotalSize 获取设备总空间 (排除系统占用空间)

场景一:计算应用缓存大小,并进行清理

1.可以通过storageStatistics.getCurrentBundleStats接口获取缓存大小。

应用的缓存文件因为级别和加密类型不同,会保存在以下目录中。例如:app级别,加密类型为el1的缓存会保存到/data/storage/el1/base/cache目录下。

/data/storage/el1/base/cache

/data/storage/el1/base/haps/entry/cache

/data/storage/el2/base/cache

/data/storage/el2/base/haps/entry/cache

这四个目录可以通过以下接口获取,其中el1与el2因为分区不同,需要切换加密等级才能获取到:

let moduleContext: common.Context;
moduleContext = this.context.createModuleContext('entry');
console.log('moduleContext + el2: '+moduleContext.cacheDir);
console.log('UIAbilityContext + el2: '+this.context.cacheDir);
moduleContext.area = contextConstant.AreaMode.EL1;
console.log('moduleContext + el1: '+moduleContext.cacheDir);
this.context.area = contextConstant.AreaMode.EL1;
console.log('UIAbilityContext + el1: '+this.context.cacheDir);

1

storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
  if (err) {
    console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info(`Invoke getCurrentBundleStats succeeded, cacheSize is ${bundleStats.cacheSize}`);
  }
});

2.清除缓存,缓存会保存在上述四个文件夹中,可以递归删除

webview缓存(/data/storage/el2/base/cache/web/Cache)包含于在上述文件夹中,也可以调用[WebviewController.removeCache]单独清理web缓存。

//通过Context.cacheDir获取所有缓存路径
let cacheDir : string[] =[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值