1# @ohos.file.storageStatistics (应用空间统计) 2<!--Kit: Core File Kit--> 3<!--Subsystem: FileManagement--> 4<!--Owner: @wang_zhangjun; @zhuangzhuang--> 5<!--Designer: @wang_zhangjun; @zhuangzhuang; @renguang1116--> 6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang--> 7<!--Adviser: @foryourself--> 8 9该模块提供空间查询相关的常用功能:包括对内外卡的空间查询、对应用分类数据统计的查询、对应用数据的查询等。 10 11> **说明:** 12> 13> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import { storageStatistics } from '@kit.CoreFileKit'; 19``` 20 21## storageStatistics.getCurrentBundleStats<sup>9+</sup> 22 23getCurrentBundleStats(): Promise<BundleStats> 24 25应用异步获取当前应用存储空间大小(单位为Byte),以Promise方式返回。 26 27**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 28 29**返回值:** 30 31 | 类型 | 说明 | 32 | ------------------------------------------ | -------------------------- | 33 | Promise<[BundleStats](#bundlestats9)> | Promise对象,返回指定卷上的应用存储空间大小(单位为Byte)。 | 34 35**错误码:** 36 37以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 38 39| 错误码ID | 错误信息 | 40| -------- | -------- | 41| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | 42| 13600001 | IPC error. | 43| 13900042 | Unknown error. | 44 45**示例:** 46 47 ```ts 48 import { BusinessError } from '@kit.BasicServicesKit'; 49 storageStatistics.getCurrentBundleStats().then((BundleStats: storageStatistics.BundleStats) => { 50 console.info("getCurrentBundleStats successfully:" + JSON.stringify(BundleStats)); 51 }).catch((err: BusinessError) => { 52 console.error("getCurrentBundleStats failed with error:"+ JSON.stringify(err)); 53 }); 54 ``` 55 56## storageStatistics.getCurrentBundleStats<sup>9+</sup> 57 58getCurrentBundleStats(callback: AsyncCallback<BundleStats>): void 59 60应用异步获取当前应用存储空间大小(单位为Byte),以callback方式返回。 61 62**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 63 64**参数:** 65 66 | 参数名 | 类型 | 必填 | 说明 | 67 | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | 68 | callback | AsyncCallback<[BundleStats](#bundlestats9)> | 是 | 获取指定卷上的应用存储空间大小之后的回调。 | 69 70**错误码:** 71 72以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 73 74| 错误码ID | 错误信息 | 75| -------- | -------- | 76| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | 77| 13600001 | IPC error. | 78| 13900042 | Unknown error. | 79 80**示例:** 81 82 ```ts 83 import { BusinessError } from '@kit.BasicServicesKit'; 84 storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => { 85 if (error) { 86 console.error("getCurrentBundleStats failed with error:" + JSON.stringify(error)); 87 } else { 88 // do something 89 console.info("getCurrentBundleStats successfully:" + JSON.stringify(bundleStats)); 90 } 91 }); 92 ``` 93 94## storageStatistics.getTotalSize<sup>15+</sup> 95 96getTotalSize(): Promise<number> 97 98获取内置存储的总空间大小(单位为Byte),以Promise方式返回。 99 100**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 101 102**返回值:** 103 104| 类型 | 说明 | 105| --------------------- | --------------------------------------------------- | 106| Promise<number> | Promise对象,返回内置存储的总空间大小(单位为Byte)。 | 107 108**错误码:** 109 110以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 111 112| 错误码ID | 错误信息 | 113| -------- | -------------- | 114| 13600001 | IPC error. | 115| 13900042 | Unknown error. | 116 117**示例:** 118 119 ```ts 120import { BusinessError } from '@kit.BasicServicesKit'; 121storageStatistics.getTotalSize().then((number: number) => { 122 console.info("getTotalSize successfully:" + JSON.stringify(number)); 123}).catch((err: BusinessError) => { 124 console.error("getTotalSize failed with error:"+ JSON.stringify(err)); 125}); 126 ``` 127 128## storageStatistics.getTotalSize<sup>15+</sup> 129 130getTotalSize(callback: AsyncCallback<number>): void 131 132获取内置存储的总空间大小(单位为Byte),以callback方式返回。 133 134**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 135 136**参数:** 137 138| 参数名 | 类型 | 必填 | 说明 | 139| -------- | --------------------------- | ---- | ---------------------------------- | 140| callback | AsyncCallback<number> | 是 | 获取内置存储的总空间大小之后的回调。 | 141 142**错误码:** 143 144以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 145 146| 错误码ID | 错误信息 | 147| -------- | ------------------------------------------------------------ | 148| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | 149| 13600001 | IPC error. | 150| 13900042 | Unknown error. | 151 152**示例:** 153 154 ```ts 155import { BusinessError } from '@kit.BasicServicesKit'; 156storageStatistics.getTotalSize((error: BusinessError, number: number) => { 157 if (error) { 158 console.error("getTotalSize failed with error:" + JSON.stringify(error)); 159 } else { 160 // do something 161 console.info("getTotalSize successfully:" + number); 162 } 163}); 164 ``` 165 166## storageStatistics.getTotalSizeSync<sup>15+</sup> 167 168getTotalSizeSync(): number 169 170同步获取内置存储的总空间大小(单位为Byte)。 171 172**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 173 174**返回值:** 175 176| 类型 | 说明 | 177| ------ | -------------------------------------- | 178| number | 返回内置存储的总空间大小(单位为Byte)。 | 179 180**错误码:** 181 182以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 183 184| 错误码ID | 错误信息 | 185| -------- | -------------- | 186| 13600001 | IPC error. | 187| 13900042 | Unknown error. | 188 189**示例:** 190 191 ```ts 192import { BusinessError } from '@kit.BasicServicesKit'; 193try { 194 let number = storageStatistics.getTotalSizeSync(); 195 console.info("getTotalSizeSync successfully:" + JSON.stringify(number)); 196} catch (err) { 197 let error: BusinessError = err as BusinessError; 198 console.error("getTotalSizeSync failed with error:" + JSON.stringify(error)); 199} 200 ``` 201 202## storageStatistics.getFreeSize<sup>15+</sup> 203 204getFreeSize(): Promise<number> 205 206获取内置存储的可用空间大小(单位为Byte),以Promise方式返回。 207 208**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 209 210**返回值:** 211 212| 类型 | 说明 | 213| --------------------- | ----------------------------------------------------- | 214| Promise<number> | Promise对象,返回内置存储的可用空间大小(单位为Byte)。 | 215 216**错误码:** 217 218以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 219 220| 错误码ID | 错误信息 | 221| -------- | -------------- | 222| 13600001 | IPC error. | 223| 13900042 | Unknown error. | 224 225**示例:** 226 227 ```ts 228import { BusinessError } from '@kit.BasicServicesKit'; 229storageStatistics.getFreeSize().then((number: number) => { 230 console.info("getFreeSize successfully:" + JSON.stringify(number)); 231}).catch((err: BusinessError) => { 232 console.error("getFreeSize failed with error:" + JSON.stringify(err)); 233}); 234 ``` 235 236## storageStatistics.getFreeSize<sup>15+</sup> 237 238getFreeSize(callback: AsyncCallback<number>): void 239 240获取内置存储的可用空间大小(单位为Byte),以callback方式返回。 241 242**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 243 244**参数:** 245 246| 参数名 | 类型 | 必填 | 说明 | 247| -------- | --------------------------- | ---- | ------------------------------------ | 248| callback | AsyncCallback<number> | 是 | 获取内置存储的可用空间大小之后的回调。 | 249 250**错误码:** 251 252以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)和[通用错误码](../errorcode-universal.md)。 253 254| 错误码ID | 错误信息 | 255| -------- | ------------------------------------------------------------ | 256| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | 257| 13600001 | IPC error. | 258| 13900042 | Unknown error. | 259 260**示例:** 261 262 ```ts 263import { BusinessError } from '@kit.BasicServicesKit'; 264storageStatistics.getFreeSize((error: BusinessError, number: number) => { 265 if (error) { 266 console.error("getFreeSize failed with error:" + JSON.stringify(error)); 267 } else { 268 // do something 269 console.info("getFreeSize successfully:" + number); 270 } 271}); 272 ``` 273 274## storageStatistics.getFreeSizeSync<sup>15+</sup> 275 276getFreeSizeSync(): number 277 278同步获取内置存储的可用空间大小(单位为Byte)。 279 280**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 281 282**返回值:** 283 284| 类型 | 说明 | 285| ------ | ---------------------------------------- | 286| number | 返回内置存储的可用空间大小(单位为Byte)。 | 287 288**错误码:** 289 290以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 291 292| 错误码ID | 错误信息 | 293| -------- | -------------- | 294| 13600001 | IPC error. | 295| 13900042 | Unknown error. | 296 297**示例:** 298 299 ```ts 300import { BusinessError } from '@kit.BasicServicesKit'; 301try { 302 let number = storageStatistics.getFreeSizeSync(); 303 console.info("getFreeSizeSync successfully:" + JSON.stringify(number)); 304} catch (err) { 305 let error: BusinessError = err as BusinessError; 306 console.error("getFreeSizeSync failed with error:" + JSON.stringify(error)); 307} 308 ``` 309 310## BundleStats<sup>9+</sup> 311 312**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 313 314| 名称 | 类型 | 必填 | 说明 | 315| --------- | ------ | --- | -------------- | 316| appSize | number | 是 | 应用安装文件大小(单位为Byte)。 | 317| cacheSize | number | 是 | 应用缓存文件大小(单位为Byte)。 | 318| dataSize | number | 是 | 应用文件存储大小(除应用安装文件)(单位为Byte)。 | 319