js-apis-storage-statistics.md 17.6 KB
Newer Older
A
annie_wangli 已提交
1 2
# App Storage Statistics

A
Annie_wang 已提交
3
> **NOTE**<br/>
A
annie_wangli 已提交
4
>
A
annie_wangli 已提交
5
> - The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
Annie_wang 已提交
6 7
> - API version 9 is a canary version for trial use. The APIs of this version may be unstable.

A
Annie_wang 已提交
8
Obtains storage space information, including the space of built-in and plug-in memory cards, space occupied by different types of data, and space of application data.
A
annie_wangli 已提交
9 10 11 12

## Modules to Import

```js
A
annie_wangli 已提交
13
import storagestatistics from "@ohos.storageStatistics";
A
annie_wangli 已提交
14 15 16 17 18 19
```

## storagestatistics.getTotalSizeOfVolume

getTotalSizeOfVolume(volumeUuid: string): Promise&lt;number&gt;

A
Annie_wang 已提交
20 21 22
Asynchronously obtains the total size of the specified volume. This API uses a promise to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER
A
annie_wangli 已提交
23

A
annie_wangli 已提交
24 25
**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

A
Annie_wang 已提交
26 27
**System API**: This is a system API and cannot be called by third-party applications.

A
annie_wangli 已提交
28 29
- Parameters

A
annie_wangli 已提交
30
  | Name    | Type  | Mandatory| Description|
A
annie_wangli 已提交
31
  | ---------- | ------ | ---- | ---- |
A
annie_wangli 已提交
32
  | volumeUuid | string | Yes  | UUID of the volume.|
A
annie_wangli 已提交
33 34 35

- Return value

A
annie_wangli 已提交
36
  | Type                 | Description            |
A
annie_wangli 已提交
37
  | --------------------- | ---------------- |
A
Annie_wang 已提交
38
  | Promise&lt;number&gt; | Promise used to return the total size of the volume.|
A
annie_wangli 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

- Example

  ```js
  let uuid = "";
  storagestatistics.getTotalSizeOfVolume(uuid).then(function(number){
      console.info("getTotalSizeOfVolume successfully:"+ number);
  }).catch(function(err){
      console.info("getTotalSizeOfVolume failed with error:"+ err);
  });
  ```

## storagestatistics.getTotalSizeOfVolume

getTotalSizeOfVolume(volumeUuid: string, callback:AsyncCallback&lt;number&gt;):void

A
Annie_wang 已提交
55 56 57
Asynchronously obtains the total size of the specified volume. This API uses a callback to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER
A
annie_wangli 已提交
58

A
annie_wangli 已提交
59 60
**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

A
Annie_wang 已提交
61 62
**System API**: This is a system API and cannot be called by third-party applications.

A
annie_wangli 已提交
63 64
- Parameters

A
annie_wangli 已提交
65
  | Name    | Type                                | Mandatory| Description                      |
A
annie_wangli 已提交
66
  | ---------- | ------------------------------------ | ---- | -------------------------- |
A
annie_wangli 已提交
67
  | volumeUuid | string                               | Yes  | UUID of the volume.                      |
A
Annie_wang 已提交
68
  | callback   | callback:AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the total size of the volume.|
A
annie_wangli 已提交
69 70 71 72 73 74

- Example

  ```js
  let uuid = "";
  storagestatistics.getTotalSizeOfVolume(uuid, function(error, number){
A
Annie_wang 已提交
75
      // Do something.
A
annie_wangli 已提交
76
      console.info("getTotalSizeOfVolume successfully:"+ number);
A
annie_wangli 已提交
77 78
  });
  ```
A
annie_wangli 已提交
79
  
A
annie_wangli 已提交
80 81 82 83 84 85
  

## storagestatistics.getFreeSizeOfVolume

getFreeSizeOfVolume(volumeUuid: string): Promise&lt;number&gt;

A
Annie_wang 已提交
86
Asynchronously obtains the available space of the specified volume. This API uses a promise to return the result.
A
annie_wangli 已提交
87

A
Annie_wang 已提交
88 89
**Required permissions**: ohos.permission.STORAGE_MANAGER

A
annie_wangli 已提交
90 91
**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

A
Annie_wang 已提交
92 93
**System API**: This is a system API and cannot be called by third-party applications.

A
annie_wangli 已提交
94 95
- Parameters

A
annie_wangli 已提交
96
  | Name    | Type  | Mandatory| Description|
A
annie_wangli 已提交
97
  | ---------- | ------ | ---- | ---- |
A
annie_wangli 已提交
98
  | volumeUuid | string | Yes  | UUID of the volume.|
A
annie_wangli 已提交
99 100 101

- Return value

A
annie_wangli 已提交
102
  | Type                 | Description              |
A
annie_wangli 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  | --------------------- | ------------------ |
  | Promise&lt;number&gt; | Promise used to return the available space of the volume.|

- Example

  ```js
  let uuid = "";
  storagestatistics.getFreeSizeOfVolume(uuid).then(function(number){
      console.info("getFreeSizeOfVolume successfully:"+ number);
  }).catch(function(err){
      console.info("getFreeSizeOfVolume failed with error:"+ err);
  });
  
  ```

## storagestatistics.getFreeSizeOfVolume

getFreeSizeOfVolume(volumeUuid: string, callback:AsyncCallback&lt;number&gt;):void

A
Annie_wang 已提交
122
Asynchronously obtains the available space of the specified volume. This API uses a callback to return the result.
A
annie_wangli 已提交
123

A
Annie_wang 已提交
124 125
**Required permissions**: ohos.permission.STORAGE_MANAGER

A
annie_wangli 已提交
126 127
**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

A
Annie_wang 已提交
128 129
**System API**: This is a system API and cannot be called by third-party applications.

A
annie_wangli 已提交
130 131
- Parameters

A
annie_wangli 已提交
132
  | Name    | Type                                | Mandatory| Description                        |
A
annie_wangli 已提交
133
  | ---------- | ------------------------------------ | ---- | ---------------------------- |
A
annie_wangli 已提交
134 135
  | volumeUuid | string                               | Yes  | UUID of the volume.                        |
  | callback   | callback:AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the available space of the volume.|
A
annie_wangli 已提交
136 137 138 139 140 141

- Example

  ```js
  let uuid = "";
  storagestatistics.getFreeSizeOfVolume(uuid, function(error, number){
A
Annie_wang 已提交
142
      // Do something.
A
annie_wangli 已提交
143
      console.info("getFreeSizeOfVolume successfully:"+ number);
A
annie_wangli 已提交
144 145 146
  });
  ```

A
annie_wangli 已提交
147
## storagestatistics.getBundleStats<sup>9+</sup>
A
annie_wangli 已提交
148

A
annie_wangli 已提交
149
getBundleStats(packageName: string): Promise&lt;BundleStats&gt;
A
annie_wangli 已提交
150

A
Annie_wang 已提交
151
Asynchronously obtains space information of an application. This API uses a promise to return the result.
A
Annie_wang 已提交
152 153

**Required permissions**: ohos.permission.STORAGE_MANAGER
A
annie_wangli 已提交
154 155

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
A
annie_wangli 已提交
156

A
Annie_wang 已提交
157 158
**System API**: This is a system API and cannot be called by third-party applications.

A
annie_wangli 已提交
159 160
- Parameters

A
annie_wangli 已提交
161
  | Name     | Type  | Mandatory| Description    |
A
annie_wangli 已提交
162
  | ----------- | ------ | ---- | -------- |
A
Annie_wang 已提交
163
  | packageName | string | Yes  | Bundle name of the application.|
A
annie_wangli 已提交
164
  
A
annie_wangli 已提交
165 166
- Return value

A
annie_wangli 已提交
167
  | Type                                      | Description                      |
A
annie_wangli 已提交
168
  | ------------------------------------------ | -------------------------- |
A
Annie_wang 已提交
169
  | Promise&lt;[Bundlestats](#bundlestats)&gt; | Promise used to return the space information obtained.|
A
annie_wangli 已提交
170 171 172 173 174

- Example

  ```js
  let packageName = "";
A
annie_wangli 已提交
175
  storagestatistics.getBundleStats(packageName).then(function(BundleStats){
A
annie_wangli 已提交
176 177 178 179 180 181
      console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats));
  }).catch(function(err){
      console.info("getBundleStats failed with error:"+ err);
  });
  ```

A
annie_wangli 已提交
182
## storagestatistics.getBundleStats<sup>9+</sup>
A
annie_wangli 已提交
183

A
annie_wangli 已提交
184
getBundleStats(packageName: string,  callback: AsyncCallback&lt;BundleStats&gt;): void
A
annie_wangli 已提交
185

A
Annie_wang 已提交
186
Asynchronously obtains space information of an application. This API uses a callback to return the result.
A
Annie_wang 已提交
187 188

**Required permissions**: ohos.permission.STORAGE_MANAGER
A
annie_wangli 已提交
189

A
annie_wangli 已提交
190
**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
A
annie_wangli 已提交
191

A
Annie_wang 已提交
192 193
**System API**: This is a system API and cannot be called by third-party applications.

A
annie_wangli 已提交
194
- Parameters
A
annie_wangli 已提交
195

A
annie_wangli 已提交
196 197
  | Name  | Type                                                     | Mandatory| Description                                |
  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
A
Annie_wang 已提交
198
  | packageName | string | Yes  | Bundle name of the application.|
A
Annie_wang 已提交
199
  | callback | callback:AsyncCallback&lt;[Bundlestats](#bundlestats)&gt; | Yes  | Callback invoked to return the space information obtained.|
A
annie_wangli 已提交
200
  
A
annie_wangli 已提交
201 202 203 204
- Example

  ```js
  let packageName = "";
A
annie_wangli 已提交
205
  storagestatistics.getBundleStats(packageName, function(error, BundleStats){
A
Annie_wang 已提交
206
      // Do something.
A
annie_wangli 已提交
207
      console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats));
A
annie_wangli 已提交
208 209 210
  });
  ```

A
Annie_wang 已提交
211 212 213 214 215 216


## storagestatistics.getCurrentBundleStats<sup>9+</sup>

getCurrentBundleStats(): Promise<BundleStats>

A
Annie_wang 已提交
217
Asynchronously obtains space information of the current third-party application. This API uses a promise to return the result.
A
Annie_wang 已提交
218 219 220 221 222 223 224

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

- Return value

  | Type                                       | Description                      |
  | ------------------------------------------ | -------------------------- |
A
Annie_wang 已提交
225
  | Promise&lt;[Bundlestats](#bundlestats)&gt; | Promise used to return the space information obtained.     |
A
Annie_wang 已提交
226 227 228 229 230 231 232 233 234 235 236 237

- Example

  ```js
      let bundleStats = await storageStatistics.getCurrentBundleStats();
      console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats));
  ```

## storagestatistics.getCurrentBundleStats<sup>9+</sup>

getCurrentBundleStats(callback: AsyncCallback<BundleStats>): void

A
Annie_wang 已提交
238 239
Asynchronously obtains space information of the current third-party application. This API uses a callback to return the result.

A
Annie_wang 已提交
240 241 242 243 244 245
**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

- Parameters

  | Name   | Type                                                      | Mandatory | Description                                |
  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
A
Annie_wang 已提交
246
  | callback | callback:AsyncCallback&lt;[BundleStats](#bundlestats)&gt; | Yes  | Callback invoked to return the space information obtained.       |
A
Annie_wang 已提交
247 248 249 250 251 252 253 254 255 256 257 258

- Example

  ```js
  storagestatistics.getCurrentBundleStats(function(error, bundleStats){
      // Do something.
      console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats));
  });
  ```
  
  
  
A
annie_wangli 已提交
259 260 261
## BundleStats<sup>9+</sup>

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
A
annie_wangli 已提交
262

A
Annie_wang 已提交
263
- Attributes
A
Annie_wang 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

| Name     | Type  | Description          |
| --------- | ------ | -------------- |
| appSize   | number | Size of the application.   |
| cacheSize | number | Cache size of the application.  |
| dataSize  | number | Total data size of the application.|




## storagestatistics.getTotalSize<sup>9+</sup>

getTotalSize(): Promise<number>

Obtains the total space of the built-in memory card. This API uses a promise to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Return value

  | Type                  | Description              |
  | --------------------- | ------------------ |
  | Promise&lt;number&gt; | Promise used to return the total space of the built-in memory card.  |

- Example

  ```js
      let number = await storageStatistics.getTotalSize();
      console.info("getTotalSize successfully:"+ JSON.stringify(number));
  ```
  
## storagestatistics.getTotalSize<sup>9+</sup>

getTotalSize(callback: AsyncCallback<number>): void

Obtains the total space of the built-in memory card. This API uses a callback to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Parameters

  | Name   | Type                                 | Mandatory | Description                    |
  | -------- | ------------------------------------ | ---- | ------------------------ |
  | callback | callback:AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the total space of the built-in memory card.|

- Example

  ```js
  storagestatistics.getTotalSize(function(error, number){
      // Do something.
      console.info("getTotalSize successfully:"+ JSON.stringify(number));
  });
  ```


## storagestatistics.getFreeSize<sup>9+</sup>

getFreeSize(): Promise<number>

Obtains the available space of the built-in memory card. This API uses a promise to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Return value

  | Type                  | Description              |
  | --------------------- | ------------------ |
  | Promise&lt;number&gt; | Promise used to return the available space of the built-in memory card.|

- Example

  ```js
      let number = await storageStatistics.getFreeSize();
      console.info("getFreeSize successfully:"+ JSON.stringify(number));
  ```


## storagestatistics.getFreeSize<sup>9+</sup>

getFreeSize(callback: AsyncCallback<number>): void

Obtains the available space of the built-in memory card. This API uses a callback to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Parameters

  | Name   | Type                                 | Mandatory| Description                      |
  | -------- | ------------------------------------ | ---- | ------------------------- |
  | callback | callback:AsyncCallback&lt;number&gt; | Yes  | Callback invoked to return the available space of the built-in memory card.|

- Example

  ```js
  storagestatistics.getFreeSize(function(error, number){
      // Do something.
      console.info("getFreeSize successfully:"+ JSON.stringify(number));
  });
  ```



## storagestatistics.getSystemSize<sup>9+</sup>

getSystemSize(): Promise&lt;number&gt;

Asynchronously obtains the system space. This API uses a promise to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Return value

  | Type                 | Description            |
  | --------------------- | ---------------- |
  | Promise&lt;number&gt; | Promise used to return the system space obtained.|

- Example

  ```js
  storagestatistics.getSystemSize().then(function(number){
      console.info("getSystemSize successfully:"+ number);
  }).catch(function(err){
      console.info("getSystemSize failed with error:"+ err);
  });
  ```

## storagestatistics.getSystemSize<sup>9+</sup>

getSystemSize(callback:AsyncCallback&lt;number&gt;):void

Asynchronously obtains the system space. This API uses a callback to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Parameters

  | Name    | Type                                | Mandatory| Description                      |
  | ---------- | ------------------------------------ | ---- | -------------------------- |
  | callback   | callback:AsyncCallback&lt;number&gt; | Yes  | Callback used to return the system space obtained.|

- Example

  ```js
  storagestatistics.getSystemSize(function(error, number){
      // Do something.
      console.info("getSystemSize successfully:"+ number);
  });
  ```
  
  
  
## storagestatistics.getUserStorageStats<sup>9+</sup>

getUserStorageStats(userId?: string): Promise&lt;StorageStats&gt;

Asynchronously obtains the space occupied by each type of user data. This API uses a promise to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Parameters

  | Name    | Type  | Mandatory| Description|
  | ---------- | ------ | ---- | ---- |
A
Annie_wang 已提交
455
  | userId | string | No  | User ID.<br>-&nbsp; Set this parameter to the ID of the user to be queried.<br>-&nbsp; If no value is specified, information about the current user is queried.|
A
Annie_wang 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

- Return value

  | Type                 | Description            |
  | --------------------- | ---------------- |
  | Promise&lt;[StorageStats](#StorageStats)&gt; | Promise used to return the information obtained.|

- Example

  ```js
  let userId = "";
  storagestatistics.getUserStorageStats(userId).then(function(StorageStats){
      console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats));
  }).catch(function(err){
      console.info("getUserStorageStats failed with error:"+ err);
  });
  ```

## storagestatistics.getUserStorageStats<sup>9+</sup>

getUserStorageStats(userId?: string, callback:AsyncCallback&lt;StorageStats&gt;):void

Asynchronously obtains the space occupied by each type of user data. This API uses a callback to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

**System API**: This is a system API and cannot be called by third-party applications.

- Parameters

  | Name    | Type                                | Mandatory| Description                      |
  | ---------- | ------------------------------------ | ---- | -------------------------- |
A
Annie_wang 已提交
490
  | userId | string                               | No  | User ID.<br>-&nbsp; Set this parameter to the ID of the user to be queried.<br>-&nbsp; If no value is specified, information about the current user is queried.                      |
A
Annie_wang 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  | callback   | callback:AsyncCallback&lt;[StorageStats](#StorageStats)&gt; | Yes  | Callback invoked to return the information obtained.|

- Example

  ```js
  storagestatistics.getUserStorageStats(userId, function(error, StorageStats){
      // Do something.
      console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats));
  });
  ```



## StorageStats<sup>9+</sup>

**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics

A
Annie_wang 已提交
508
- Attributes
A
annie_wangli 已提交
509

A
annie_wangli 已提交
510
| Name     | Type  | Description          |
A
annie_wangli 已提交
511
| --------- | ------ | -------------- |
A
Annie_wang 已提交
512
| total   | number | Total space of the built-in memory card.   |
A
Annie_wang 已提交
513 514 515
| audio | number | Space occupied by audio data.  |
| video  | number | Space occupied by video data.|
| image   | number | Space occupied by image data.   |
A
Annie_wang 已提交
516
| file | number | Space occupied by files.  |
A
Annie_wang 已提交
517
| app  | number | Space occupied by application data.|