1# @ohos.file.storageStatistics (应用空间统计) 2 3该模块提供空间查询相关的常用功能:包括对内外卡的空间查询,对应用分类数据统计的查询,对应用数据的查询等。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 本模块支持对错误码进行处理,错误码及其适配方式[参考文档](../errorcodes/errorcode-filemanagement.md#错误码适配指导)。 9## 导入模块 10 11```js 12import storageStatistics from "@ohos.file.storageStatistics"; 13``` 14 15## storageStatistics.getTotalSizeOfVolume 16 17getTotalSizeOfVolume(volumeUuid: string): Promise<number> 18 19异步获取外置存储设备中指定卷设备的总空间大小(单位为Byte),以promise方式返回。 20 21**需要权限**:ohos.permission.STORAGE_MANAGER 22 23**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 24 25**系统接口:** 该接口为系统接口。 26 27 28**参数:** 29 30 | 参数名 | 类型 | 必填 | 说明 | 31 | ---------- | ------ | ---- | ---- | 32 | volumeUuid | string | 是 | 卷设备uuid | 33 34**返回值:** 35 36 | 类型 | 说明 | 37 | --------------------- | ---------------- | 38 | Promise<number> | 返回指定卷设备的总空间大小(单位为Byte) | 39 40**示例:** 41 42 ```js 43 let uuid = ""; 44 storageStatistics.getTotalSizeOfVolume(uuid).then(function(number){ 45 console.info("getTotalSizeOfVolume successfully:"+ number); 46 }).catch(function(err){ 47 console.info("getTotalSizeOfVolume failed with error:"+ err); 48 }); 49 ``` 50 51## storageStatistics.getTotalSizeOfVolume 52 53getTotalSizeOfVolume(volumeUuid: string, callback: AsyncCallback<number>): void 54 55异步获取外置存储设备中指定卷设备的总空间大小(单位为Byte),以callback方式返回。 56 57**需要权限**:ohos.permission.STORAGE_MANAGER 58 59**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 60 61**系统接口:** 该接口为系统接口。 62 63 64**参数:** 65 66 | 参数名 | 类型 | 必填 | 说明 | 67 | ---------- | ------------------------------------ | ---- | -------------------------- | 68 | volumeUuid | string | 是 | 卷设备uuid | 69 | callback | AsyncCallback<number> | 是 | 获取指定卷设备总空间之后的回调 | 70 71**示例:** 72 73 ```js 74 let uuid = ""; 75 storageStatistics.getTotalSizeOfVolume(uuid, function(error, number){ 76 // do something 77 console.info("getTotalSizeOfVolume successfully:"+ number); 78 }); 79 ``` 80 81## storageStatistics.getFreeSizeOfVolume 82 83getFreeSizeOfVolume(volumeUuid: string): Promise<number> 84 85异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以promise方式返回。 86 87**需要权限**:ohos.permission.STORAGE_MANAGER 88 89**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 90 91**系统接口:** 该接口为系统接口。 92 93 94**参数:** 95 96 | 参数名 | 类型 | 必填 | 说明 | 97 | ---------- | ------ | ---- | ---- | 98 | volumeUuid | string | 是 | 卷设备uuid | 99 100**返回值:** 101 102 | 类型 | 说明 | 103 | --------------------- | ------------------ | 104 | Promise<number> | 返回指定卷的可用空间大小(单位为Byte) | 105 106**示例:** 107 108 ```js 109 let uuid = ""; 110 storageStatistics.getFreeSizeOfVolume(uuid).then(function(number){ 111 console.info("getFreeSizeOfVolume successfully:"+ number); 112 }).catch(function(err){ 113 console.info("getFreeSizeOfVolume failed with error:"+ err); 114 }); 115 116 ``` 117 118## storageStatistics.getFreeSizeOfVolume 119 120getFreeSizeOfVolume(volumeUuid: string, callback: AsyncCallback<number>): void 121 122异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以callback方式返回。 123 124**需要权限**:ohos.permission.STORAGE_MANAGER 125 126**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 127 128**系统接口:** 该接口为系统接口。 129 130 131**参数:** 132 133 | 参数名 | 类型 | 必填 | 说明 | 134 | ---------- | ------------------------------------ | ---- | ---------------------------- | 135 | volumeUuid | string | 是 | 卷设备uuid | 136 | callback | AsyncCallback<number> | 是 | 获取指定卷可用空间之后的回调 | 137 138**示例:** 139 140 ```js 141 let uuid = ""; 142 storageStatistics.getFreeSizeOfVolume(uuid, function(error, number){ 143 // do something 144 console.info("getFreeSizeOfVolume successfully:"+ number); 145 }); 146 ``` 147 148## storageStatistics.getBundleStats<sup>9+</sup> 149 150getBundleStats(packageName: string): Promise<BundleStats> 151 152异步获取应用存储空间大小(单位为Byte),以promise方式返回。 153 154**需要权限**:ohos.permission.STORAGE_MANAGER 155 156**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 157 158**系统接口:** 该接口为系统接口。 159 160 161**参数:** 162 163 | 参数名 | 类型 | 必填 | 说明 | 164 | ----------- | ------ | ---- | -------- | 165 | packageName | string | 是 | 应用包名 | 166 167**返回值:** 168 169 | 类型 | 说明 | 170 | ------------------------------------------ | -------------------------- | 171 | Promise<[Bundlestats](#bundlestats9)> | 返回指定卷上的应用存储数据(单位为Byte) | 172 173**示例:** 174 175 ```js 176 let packageName = ""; 177 storageStatistics.getBundleStats(packageName).then(function(BundleStats){ 178 console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats)); 179 }).catch(function(err){ 180 console.info("getBundleStats failed with error:"+ err); 181 }); 182 ``` 183 184## storageStatistics.getBundleStats<sup>9+</sup> 185 186getBundleStats(packageName: string, callback: AsyncCallback<BundleStats>): void 187 188异步获取应用存储空间大小(单位为Byte),以callback方式返回。 189 190**需要权限**:ohos.permission.STORAGE_MANAGER 191 192**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 193 194**系统接口:** 该接口为系统接口。 195 196 197**参数:** 198 199 | 参数名 | 类型 | 必填 | 说明 | 200 | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | 201 | packageName | string | 是 | 应用包名 | 202 | callback | AsyncCallback<[Bundlestats](#bundlestats9)> | 是 | 获取指定卷上的应用存储数据之后的回调 | 203 204**示例:** 205 206 ```js 207 let packageName = ""; 208 storageStatistics.getBundleStats(packageName, function(error, BundleStats){ 209 // do something 210 console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats)); 211 }); 212 ``` 213 214## storageStatistics.getCurrentBundleStats<sup>9+</sup> 215 216getCurrentBundleStats(): Promise<BundleStats> 217 218第三方应用异步获取当前应用存储空间大小(单位为Byte),以promise方式返回。 219 220**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 221 222**返回值:** 223 224 | 类型 | 说明 | 225 | ------------------------------------------ | -------------------------- | 226 | Promise<[Bundlestats](#bundlestats9)> | 返回指定卷上的应用存空间大小(单位为Byte) | 227 228**示例:** 229 230 ```js 231 let bundleStats = storageStatistics.getCurrentBundleStats(); 232 console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats)); 233 ``` 234 235## storageStatistics.getCurrentBundleStats<sup>9+</sup> 236 237getCurrentBundleStats(callback: AsyncCallback<BundleStats>): void 238 239第三方应用异步获取当前应用存储空间大小(单位为Byte),以callback方式返回。 240 241**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 242 243**参数:** 244 245 | 参数名 | 类型 | 必填 | 说明 | 246 | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | 247 | callback | AsyncCallback<[BundleStats](#bundlestats9)> | 是 | 获取指定卷上的应用存储空间大小之后的回调 | 248 249**示例:** 250 251 ```js 252 storageStatistics.getCurrentBundleStats(function(error, bundleStats){ 253 // do something 254 console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats)); 255 }); 256 ``` 257 258## BundleStats<sup>9+</sup> 259 260**系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.StorageService.SpatialStatistics 261 262| 名称 | 类型 | 可读 | 可写 | 说明 | 263| --------- | ------ | --- | ---- | -------------- | 264| appSize | number | 是 | 否 | app数据大小(单位为Byte) | 265| cacheSize | number | 是 | 否 | 缓存数据大小(单位为Byte) | 266| dataSize | number | 是 | 否 | 应用总数据大小(单位为Byte) | 267 268 269## storageStatistics.getTotalSize<sup>9+</sup> 270 271getTotalSize(): Promise<number> 272 273获取内置存储的总空间大小(单位为Byte),以promise方式返回。 274 275**需要权限**:ohos.permission.STORAGE_MANAGER 276 277**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 278 279**系统接口:** 该接口为系统接口。 280 281**返回值:** 282 283 | 类型 | 说明 | 284 | --------------------- | ------------------ | 285 | Promise<number> | 返回内置存储的总空间大小(单位为Byte) | 286 287**示例:** 288 289 ```js 290 let number = storageStatistics.getTotalSize(); 291 console.info("getTotalSize successfully:"+ JSON.stringify(number)); 292 ``` 293 294## storageStatistics.getTotalSize<sup>9+</sup> 295 296getTotalSize(callback: AsyncCallback<number>): void 297 298获取内置存储的总空间大小(单位为Byte),以callback方式返回。 299 300**需要权限**:ohos.permission.STORAGE_MANAGER 301 302**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 303 304**系统接口:** 该接口为系统接口。 305 306**参数:** 307 308 | 参数名 | 类型 | 必填 | 说明 | 309 | -------- | ------------------------------------ | ---- | ------------------------ | 310 | callback | AsyncCallback<number> | 是 | 获取内置存储的总空间大小之后的回调 | 311 312**示例:** 313 314 ```js 315 storageStatistics.getTotalSize(function(error, number){ 316 // do something 317 console.info("getTotalSize successfully:"+ JSON.stringify(number)); 318 }); 319 ``` 320 321 322## storageStatistics.getFreeSize<sup>9+</sup> 323 324getFreeSize(): Promise<number> 325 326获取内置存储的可用空间大小(单位为Byte),以promise方式返回。 327 328**需要权限**:ohos.permission.STORAGE_MANAGER 329 330**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 331 332**系统接口:** 该接口为系统接口。 333 334**返回值:** 335 336 | 类型 | 说明 | 337 | --------------------- | ------------------ | 338 | Promise<number> | 返回内置存储的可用空间大小(单位为Byte) | 339 340**示例:** 341 342 ```js 343 let number = storageStatistics.getFreeSize(); 344 console.info("getFreeSize successfully:"+ JSON.stringify(number)); 345 ``` 346 347 348## storageStatistics.getFreeSize<sup>9+</sup> 349 350getFreeSize(callback: AsyncCallback<number>): void 351 352获取内置存储的可用空间大小(单位为Byte),以callback方式返回。 353 354**需要权限**:ohos.permission.STORAGE_MANAGER 355 356**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 357 358**系统接口:** 该接口为系统接口。 359 360**参数:** 361 362 | 参数名 | 类型 | 必填 | 说明 | 363 | -------- | ------------------------------------ | ---- | ------------------------- | 364 | callback | AsyncCallback<number> | 是 | 获取内置存储的可用空间大小之后的回调 | 365 366**示例:** 367 368 ```js 369 storageStatistics.getFreeSize(function(error, number){ 370 // do something 371 console.info("getFreeSize successfully:"+ JSON.stringify(number)); 372 }); 373 ``` 374 375## storageStatistics.getSystemSize<sup>9+</sup> 376 377getSystemSize(): Promise<number> 378 379异步获取系统数据的空间大小(单位为Byte),以promise方式返回。 380 381**需要权限**:ohos.permission.STORAGE_MANAGER 382 383**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 384 385**系统接口:** 该接口为系统接口。 386 387 388**返回值:** 389 390 | 类型 | 说明 | 391 | --------------------- | ---------------- | 392 | Promise<number> | 返回系统数据的空间大小(单位为Byte) | 393 394**示例:** 395 396 ```js 397 storageStatistics.getSystemSize().then(function(number){ 398 console.info("getSystemSize successfully:"+ number); 399 }).catch(function(err){ 400 console.info("getSystemSize failed with error:"+ err); 401 }); 402 ``` 403 404## storageStatistics.getSystemSize<sup>9+</sup> 405 406getSystemSize(callback: AsyncCallback<number>): void 407 408异步获取系统数据的空间大小(单位为Byte),以callback方式返回。 409 410**需要权限**:ohos.permission.STORAGE_MANAGER 411 412**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 413 414**系统接口:** 该接口为系统接口。 415 416**参数:** 417 418 | 参数名 | 类型 | 必填 | 说明 | 419 | ---------- | ------------------------------------ | ---- | -------------------------- | 420 | callback | AsyncCallback<number> | 是 | 获取系统数据的空间大小之后的回调 | 421 422**示例:** 423 424 ```js 425 storageStatistics.getSystemSize(function(error, number){ 426 // do something 427 console.info("getSystemSize successfully:"+ number); 428 }); 429 ``` 430 431## storageStatistics.getUserStorageStats<sup>9+</sup> 432 433getUserStorageStats(): Promise<StorageStats> 434 435异步获取当前用户各类别存储空间大小(单位为Byte),以promise方式返回。 436 437**需要权限**:ohos.permission.STORAGE_MANAGER 438 439**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 440 441**系统接口:** 该接口为系统接口。 442 443**返回值:** 444 445 | 类型 | 说明 | 446 | --------------------- | ---------------- | 447 | Promise<[StorageStats](#storagestats9)> | 返回当前用户各类别存储空间大小(单位为Byte) | 448 449**示例:** 450 451 ```js 452 storageStatistics.getUserStorageStats().then(function(StorageStats){ 453 console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); 454 }).catch(function(err){ 455 console.info("getUserStorageStats failed with error:"+ err); 456 }); 457 ``` 458 459## storageStatistics.getUserStorageStats<sup>9+</sup> 460 461getUserStorageStats(callback: AsyncCallback<StorageStats>): void 462 463异步获取当前用户各类别存储空间大小(单位为Byte),以callback方式返回。 464 465**需要权限**:ohos.permission.STORAGE_MANAGER 466 467**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 468 469**系统接口:** 该接口为系统接口。 470 471**参数:** 472 473 | 参数名 | 类型 | 必填 | 说明 | 474 | ---------- | ------------------------------------ | ---- | -------------------------- | 475 | callback | AsyncCallback<[StorageStats](#storagestats9)> | 是 | 返回用户各类别存储空间大小之后的回调 | 476 477**示例:** 478 479 ```js 480 storageStatistics.getUserStorageStats(function(error, StorageStats){ 481 // do something 482 console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); 483 }); 484 ``` 485getUserStorageStats(userId: number): Promise<StorageStats> 486 487异步获取指定用户各类别存储空间大小(单位为Byte),以promise方式返回。 488 489**需要权限**:ohos.permission.STORAGE_MANAGER 490 491**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 492 493**系统接口:** 该接口为系统接口。 494 495**参数:** 496 497 | 参数名 | 类型 | 必填 | 说明 | 498 | ---------- | ------ | ---- | ---- | 499 | userId | number | 是 | 用户id| 500 501**返回值:** 502 503 | 类型 | 说明 | 504 | --------------------- | ---------------- | 505 | Promise<[StorageStats](#storagestats9)> | 返回指定用户各类别存储空间大小(单位为Byte) | 506 507**示例:** 508 509 ```js 510 let userId = 100; 511 storageStatistics.getUserStorageStats(userId).then(function(StorageStats){ 512 console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); 513 }).catch(function(err){ 514 console.info("getUserStorageStats failed with error:"+ err); 515 }); 516 ``` 517 518## storageStatistics.getUserStorageStats<sup>9+</sup> 519 520getUserStorageStats(userId: number, callback: AsyncCallback<StorageStats>): void 521 522异步获取指定用户各类别存储空间大小(单位为Byte),以callback方式返回。 523 524**需要权限**:ohos.permission.STORAGE_MANAGER 525 526**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 527 528**系统接口:** 该接口为系统接口。 529 530**参数:** 531 532 | 参数名 | 类型 | 必填 | 说明 | 533 | ---------- | ------------------------------------ | ---- | -------------------------- | 534 | userId | number | 是 | 用户id | 535 | callback | AsyncCallback<[StorageStats](#storagestats9)> | 是 | 返回各类别数据大小之后的回调 | 536 537**示例:** 538 539 ```js 540 let userId = 100; 541 storageStatistics.getUserStorageStats(userId, function(error, StorageStats){ 542 // do something 543 console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); 544 }); 545 ``` 546 547 548## StorageStats<sup>9+</sup> 549 550**系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.StorageService.SpatialStatistics 551 552**系统接口:** 该接口为系统接口。 553 554| 名称 | 类型 | 可读 | 可写 | 说明 | 555| --------- | ------ | ---- | ----- | -------------- | 556| total | number | 是 | 否 | 内置存储总空间大小(单位为Byte) | 557| audio | number |是 | 否 | 音频数据大小 (单位为Byte) | 558| video | number | 是 | 否 | 视频数据大小(单位为Byte) | 559| image | number | 是 | 否 | 图像数据大小 (单位为Byte) | 560| file | number | 是 | 否 | 文件数据大小 (单位为Byte) | 561| app | number | 是 | 否 | 应用数据大小(单位为Byte) | 562