HarmonyOS鸿蒙Next中使用storageStatistics.getCurrentBundleStats()获取APP应用缓存大小失效
HarmonyOS鸿蒙Next中使用storageStatistics.getCurrentBundleStats()获取APP应用缓存大小失效
咨询描述:
需求期望获取到开发的APP缓存空间存储占用大小,目前使用storageStatistics.getCurrentBundleStats API来获取,但是调试发现更新手机系统后获取到的bundleStats.cacheSize=0。(已确认沙箱中cache目录下是存在缓存文件的,非空)
获取缓存代码如下:
```typescript
getCache() {
storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
if (error) {
console.error('getCurrentBundleStats failed with error:' + JSON.stringify(error));
} else {
console.info('getCurrentBundleStats successfully:' + JSON.stringify(bundleStats));
console.info('appsize :' + bundleStats.appSize);
console.info('cacheSize :' + bundleStats.cacheSize);
console.info('dataSize :' + bundleStats.dataSize);
this.curCacheSize = bundleStats.cacheSize / 1024 / 1024 + 'M';
}
});
}
工程机版本:NEXT.0.0.71(SP6C00E71R4P17logpatch01)
DevEco Studio版本:Build Version: 5.0.3.800, built on September 4, 2024
SDK版本:OpenHarmony 5.0.0.25
更多关于HarmonyOS鸿蒙Next中使用storageStatistics.getCurrentBundleStats()获取APP应用缓存大小失效的实战教程也可以访问 https://www.itying.com/category-93-b0.html
模拟器可以,真机获取到的0,等修复吧
更多关于HarmonyOS鸿蒙Next中使用storageStatistics.getCurrentBundleStats()获取APP应用缓存大小失效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
我也遇到了这个问题,给华为提了工单 还没回复
楼主你好,确认下cache目录是否有文件,文件大小是否为0.本地验证过正常可以读取,
这是我的代码,由于我的cache目录下没有文件,所以我写了一个test,我的手机版本是最新的,
curCacheSize: string = ''
aboutToAppear(): void {
let mContext = getContext(this);
let filePath = mContext.cacheDir + "/test.txt";
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
let str: string = "hello, world";
let writeLen = fs.writeSync(file.fd, str);
console.info("write data to file succeed and size is:" + writeLen);
fs.closeSync(file);
storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
if (error) {
console.error('getCurrentBundleStats failed with error:' + JSON.stringify(error));
} else {
console.info('getCurrentBundleStats successfully:' + JSON.stringify(bundleStats));
console.info('getCurrentBundleStats appsize :' + bundleStats.appSize);
console.info('getCurrentBundleStats cacheSize :' + bundleStats.cacheSize);
console.info('getCurrentBundleStats dataSize :' + bundleStats.dataSize);
this.curCacheSize = bundleStats.cacheSize / 1024 / 1024 + 'M';
console.info('getCurrentBundleStats this.curCacheSize :' + this.curCacheSize);
}
});


在HarmonyOS鸿蒙Next中,storageStatistics.getCurrentBundleStats()方法用于获取当前应用的存储统计信息。如果该方法失效,可能是由于以下几个原因:
-
权限问题:确保应用已经获取了必要的存储权限。鸿蒙系统对权限管理较为严格,未授予相关权限可能导致方法调用失败。
-
API版本兼容性:检查使用的API版本是否与当前鸿蒙系统版本兼容。不同版本的系统可能对API的实现有所不同,导致方法失效。
-
应用状态:确保应用在调用该方法时处于正常运行状态。如果应用处于后台或未启动状态,可能会导致方法调用失败。
-
系统缓存更新延迟:系统缓存信息的更新可能存在延迟,导致获取的缓存大小不准确。可以尝试多次调用或等待系统更新缓存信息。
-
方法调用时机:
getCurrentBundleStats()方法需要在合适的时机调用,例如在应用启动或数据更新后调用,以确保获取到最新的缓存信息。 -
系统限制:某些系统版本或设备可能对
getCurrentBundleStats()方法的使用存在限制,导致方法失效。可以查阅相关文档或日志以获取更多信息。 -
应用缓存管理:如果应用自身对缓存进行了特殊管理,可能会影响
getCurrentBundleStats()方法的调用结果。
总之,storageStatistics.getCurrentBundleStats()方法失效可能与权限、API版本、应用状态、系统缓存更新、方法调用时机、系统限制或应用缓存管理有关。
在HarmonyOS鸿蒙Next中,storageStatistics.getCurrentBundleStats() 方法用于获取当前应用的存储统计信息,包括缓存大小。如果该方法失效,可能是以下原因:
- 权限问题:确保应用已获取
ohos.permission.STORAGE_MANAGER权限。 - API变更:检查是否使用了正确的API版本,鸿蒙Next可能有API更新或废弃。
- 应用上下文:确保调用方法时应用上下文正确,避免在错误的生命周期阶段调用。
- 系统限制:某些设备或系统版本可能限制了该功能。
建议查阅最新的HarmonyOS开发文档,或使用替代方法如 storageStatistics.getBundleStats() 获取存储信息。

