HarmonyOS鸿蒙Next中storageStatistics.getCurrentBundleStats()获取文件大小有问题

HarmonyOS鸿蒙Next中storageStatistics.getCurrentBundleStats()获取文件大小有问题

storageStatistics.getCurrentBundleStats() 获取的文件大小有问题

async clearCache() {
    if (await this.clearCacheConfirm()) {
        showLoading('清理缓存中...')
        
        let cacheDir = getContext(this).cacheDir;
        fileIo.rmdirSync(cacheDir);
        
        fileIo.listFile(cacheDir).then((filenames) => {
            for (let i = 0; i < filenames.length; i++) {
                let dirPath = cacheDir + filenames[i];
                // 判断是否为文件夹
                let isDirectory: boolean = false;
                try {
                    isDirectory = fileIo.statSync(dirPath).isDirectory();
                } catch (e) {
                    console.log(e);
                }
                
                if (isDirectory) {
                    fileIo.rmdirSync(dirPath);
                } else {
                    fileIo.unlink(dirPath).then(() => {
                        console.info('remove file succeed');
                    }).catch((err: Error) => {
                        console.info('remove file failed with error message: ' + err.message);
                    });
                }
            }
            
        })
        
        dismissLoading()
        showLoading('清除成功')
        await delay(1000)
        this.cacheSizeText = FormatUtils.byteNumberFormat(0) //同android保持一致
        dismissLoading()
    }
}

通过这个方法删除了对应文件夹之后, 再通过storageStatistics.getCurrentBundleStats()获取这个文件夹的大小, 结果还是有值


更多关于HarmonyOS鸿蒙Next中storageStatistics.getCurrentBundleStats()获取文件大小有问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

storageStatistics.getCurrentBundleStats()这个接口获取当前应用存储空间大小(单位为Byte),并不是文件夹的大小,结果有值是正常现象。

您是想获取文件夹的大小吗?

可通过下面demo获取文件夹大小

function getFileSize(path:string):Number{
  let size = 0
  class ListFileOption { //制定筛选规则
    public recursion: boolean = true; //true为获取该文件夹下所有文件
    public listNum: number = 0; //获取文件的数量,为0时获取所有文件
  }
  let option = new ListFileOption()
  let filenames = fs.listFileSync(path,option)

  for(let i =0;i<filenames.length;i++){
    size += fs.statSync(path+filenames[i]).size
  }
  return size
}

更多关于HarmonyOS鸿蒙Next中storageStatistics.getCurrentBundleStats()获取文件大小有问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,storageStatistics.getCurrentBundleStats()方法用于获取当前应用的存储统计信息,包括文件大小。如果获取的文件大小有问题,可能是由于以下原因:

  1. 缓存数据未更新:获取的文件大小可能未及时更新,导致显示的数据不准确。可以尝试手动刷新或重新调用方法。

  2. 权限问题:应用可能缺少必要的存储权限,导致无法正确获取文件大小。确保应用已获取了所需的权限。

  3. 文件系统问题:文件系统可能存在异常,导致统计信息不准确。可以检查文件系统状态或重启设备。

  4. API限制:getCurrentBundleStats()方法可能在某些场景下存在限制或bug,导致获取的数据不准确。可以查看相关文档或更新到最新版本。

  5. 多用户环境:在多用户环境下,获取的文件大小可能仅针对当前用户,而不是整个应用的所有用户数据。

  6. 异步操作:如果获取文件大小的操作是异步的,可能在数据还未完全加载时就被返回,导致数据不准确。

这些问题可能导致storageStatistics.getCurrentBundleStats()获取的文件大小不准确,需要根据具体情况进行排查。

在HarmonyOS鸿蒙Next中,storageStatistics.getCurrentBundleStats() 用于获取当前应用的存储统计信息。若获取的文件大小存在问题,可能是由于以下原因:

  1. 缓存数据未更新:系统缓存可能导致数据不准确,尝试重启设备或清除缓存后重新获取。
  2. 权限不足:确保应用已获取必要的存储权限,如 ohos.permission.READ_MEDIA
  3. API限制:部分API可能存在限制,建议检查官方文档或更新SDK版本。
  4. 文件系统状态:文件系统可能处于异常状态,建议检查存储设备是否正常。

如果问题持续,建议提供更多上下文信息或日志,以便进一步排查。

回到顶部