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

A
Annie_wang 已提交
3 4
The storageStatistics module provides APIs for obtaining 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_wang 已提交
5
> **NOTE**<br/>
A
annie_wangli 已提交
6
>
A
annie_wangli 已提交
7
> - 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 已提交
8 9
> - API version 9 is a canary version for trial use. The APIs of this version may be unstable.

A
annie_wangli 已提交
10 11 12
## Modules to Import

```js
A
Annie_wang 已提交
13
import storageStatistics from "@ohos.storageStatistics";
A
annie_wangli 已提交
14 15
```

A
Annie_wang 已提交
16
## storageStatistics.getTotalSizeOfVolume
A
annie_wangli 已提交
17 18 19

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 28 29 30

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


**Parameters**
A
annie_wangli 已提交
31

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

A
Annie_wang 已提交
36
**Return value**
A
annie_wangli 已提交
37

A
annie_wangli 已提交
38
  | Type                 | Description            |
A
annie_wangli 已提交
39
  | --------------------- | ---------------- |
A
Annie_wang 已提交
40
  | Promise&lt;number&gt; | Promise used to return the total size of the volume.|
A
annie_wangli 已提交
41

A
Annie_wang 已提交
42
**Example**
A
annie_wangli 已提交
43 44 45

  ```js
  let uuid = "";
A
Annie_wang 已提交
46
  storageStatistics.getTotalSizeOfVolume(uuid).then(function(number){
A
annie_wangli 已提交
47 48 49 50 51 52
      console.info("getTotalSizeOfVolume successfully:"+ number);
  }).catch(function(err){
      console.info("getTotalSizeOfVolume failed with error:"+ err);
  });
  ```

A
Annie_wang 已提交
53
## storageStatistics.getTotalSizeOfVolume
A
annie_wangli 已提交
54 55 56

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

A
Annie_wang 已提交
57 58 59
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 已提交
60

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

A
Annie_wang 已提交
63 64 65 66 67

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


**Parameters**
A
annie_wangli 已提交
68

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

A
Annie_wang 已提交
74
**Example**
A
annie_wangli 已提交
75 76 77

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

A
Annie_wang 已提交
84
## storageStatistics.getFreeSizeOfVolume
A
annie_wangli 已提交
85 86 87

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

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

A
Annie_wang 已提交
90 91
**Required permissions**: ohos.permission.STORAGE_MANAGER

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

A
Annie_wang 已提交
94 95 96 97 98

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


**Parameters**
A
annie_wangli 已提交
99

A
annie_wangli 已提交
100
  | Name    | Type  | Mandatory| Description|
A
annie_wangli 已提交
101
  | ---------- | ------ | ---- | ---- |
A
annie_wangli 已提交
102
  | volumeUuid | string | Yes  | UUID of the volume.|
A
annie_wangli 已提交
103

A
Annie_wang 已提交
104
**Return value**
A
annie_wangli 已提交
105

A
annie_wangli 已提交
106
  | Type                 | Description              |
A
annie_wangli 已提交
107 108 109
  | --------------------- | ------------------ |
  | Promise&lt;number&gt; | Promise used to return the available space of the volume.|

A
Annie_wang 已提交
110
**Example**
A
annie_wangli 已提交
111 112 113

  ```js
  let uuid = "";
A
Annie_wang 已提交
114
  storageStatistics.getFreeSizeOfVolume(uuid).then(function(number){
A
annie_wangli 已提交
115 116 117 118 119 120 121
      console.info("getFreeSizeOfVolume successfully:"+ number);
  }).catch(function(err){
      console.info("getFreeSizeOfVolume failed with error:"+ err);
  });
  
  ```

A
Annie_wang 已提交
122
## storageStatistics.getFreeSizeOfVolume
A
annie_wangli 已提交
123 124 125

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

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

A
Annie_wang 已提交
128 129
**Required permissions**: ohos.permission.STORAGE_MANAGER

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

A
Annie_wang 已提交
132 133 134 135 136

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


**Parameters**
A
annie_wangli 已提交
137

A
annie_wangli 已提交
138
  | Name    | Type                                | Mandatory| Description                        |
A
annie_wangli 已提交
139
  | ---------- | ------------------------------------ | ---- | ---------------------------- |
A
annie_wangli 已提交
140 141
  | 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 已提交
142

A
Annie_wang 已提交
143
**Example**
A
annie_wangli 已提交
144 145 146

  ```js
  let uuid = "";
A
Annie_wang 已提交
147
  storageStatistics.getFreeSizeOfVolume(uuid, function(error, number){
A
Annie_wang 已提交
148
      // Do something.
A
annie_wangli 已提交
149
      console.info("getFreeSizeOfVolume successfully:"+ number);
A
annie_wangli 已提交
150 151
  });
  ```
A
Annie_wang 已提交
152

A
Annie_wang 已提交
153
## storageStatistics.getBundleStats<sup>9+</sup>
A
Annie_wang 已提交
154 155 156

getBundleStats(packageName: string): Promise&lt;BundleStats&gt;

A
Annie_wang 已提交
157 158 159
Asynchronously obtains space information of an application. This API uses a promise to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER
A
Annie_wang 已提交
160 161 162

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

A
Annie_wang 已提交
163 164 165 166 167

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


**Parameters**
A
Annie_wang 已提交
168 169 170

  | Name     | Type  | Mandatory| Description    |
  | ----------- | ------ | ---- | -------- |
A
Annie_wang 已提交
171
  | packageName | string | Yes  | Bundle name of the application.|
A
Annie_wang 已提交
172 173

**Return value**
A
Annie_wang 已提交
174 175 176

  | Type                                      | Description                      |
  | ------------------------------------------ | -------------------------- |
A
Annie_wang 已提交
177
  | Promise&lt;[Bundlestats](#bundlestats)&gt; | Promise used to return the space information obtained.|
A
Annie_wang 已提交
178

A
Annie_wang 已提交
179
**Example**
A
Annie_wang 已提交
180 181 182

  ```js
  let packageName = "";
A
Annie_wang 已提交
183
  storageStatistics.getBundleStats(packageName).then(function(BundleStats){
A
Annie_wang 已提交
184 185 186 187 188 189
      console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats));
  }).catch(function(err){
      console.info("getBundleStats failed with error:"+ err);
  });
  ```

A
Annie_wang 已提交
190
## storageStatistics.getBundleStats<sup>9+</sup>
A
Annie_wang 已提交
191 192 193

getBundleStats(packageName: string,  callback: AsyncCallback&lt;BundleStats&gt;): void

A
Annie_wang 已提交
194 195 196
Asynchronously obtains space information of an application. This API uses a callback to return the result.

**Required permissions**: ohos.permission.STORAGE_MANAGER
A
Annie_wang 已提交
197 198 199

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

A
Annie_wang 已提交
200 201 202 203 204

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


**Parameters**
A
Annie_wang 已提交
205 206 207

  | Name  | Type                                                     | Mandatory| Description                                |
  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
A
Annie_wang 已提交
208
  | packageName | string | Yes  | Bundle name of the application.|
A
Annie_wang 已提交
209 210 211
  | callback | callback:AsyncCallback&lt;[Bundlestats](#bundlestats)&gt; | Yes  | Callback invoked to return the space information obtained.|

**Example**
A
Annie_wang 已提交
212 213 214

  ```js
  let packageName = "";
A
Annie_wang 已提交
215
  storageStatistics.getBundleStats(packageName, function(error, BundleStats){
A
Annie_wang 已提交
216 217 218 219 220
      // Do something.
      console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats));
  });
  ```

A
Annie_wang 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
## storageStatistics.getCurrentBundleStats<sup>9+</sup>

getCurrentBundleStats(): Promise&lt;BundleStats&gt;

Asynchronously obtains space information of the current third-party application. This API uses a promise to return the result.

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

**Return value**

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

**Example**

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

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

getCurrentBundleStats(callback: AsyncCallback&lt;BundleStats&gt;): void

Asynchronously obtains space information of the current third-party application. This API uses a callback to return the result.

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

**Parameters**

  | Name   | Type                                                      | Mandatory | Description                                |
  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
  | callback | callback:AsyncCallback&lt;[BundleStats](#bundlestats)&gt; | Yes  | Callback invoked to return the space information obtained.       |

**Example**

  ```js
  storageStatistics.getCurrentBundleStats(function(error, bundleStats){
      // Do something.
      console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats));
  });
  ```

A
Annie_wang 已提交
265 266 267 268
## BundleStats<sup>9+</sup>

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

A
Annie_wang 已提交
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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

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


### Attributes

| 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&lt;number&gt;

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


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 = storageStatistics.getTotalSize();
  console.info("getTotalSize successfully:"+ JSON.stringify(number));
  ```

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

getTotalSize(callback: AsyncCallback&lt;number&gt;): 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


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&lt;number&gt;

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


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 = storageStatistics.getFreeSize();
  console.info("getFreeSize successfully:"+ JSON.stringify(number));
  ```


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

getFreeSize(callback: AsyncCallback&lt;number&gt;): 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


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


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


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? : number): 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


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


**Parameters**

  | Name    | Type  | Mandatory| Description|
  | ---------- | ------ | ---- | ---- |
A
Annie_wang 已提交
473
  | userId | number | No  | User ID.<br>Value:<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 已提交
474 475 476 477 478 479 480 481 482 483

**Return value**

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

**Example**

  ```js
A
Annie_wang 已提交
484
  let userId = 1;
A
Annie_wang 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
  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: number, 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


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


**Parameters**

  | Name    | Type                                | Mandatory| Description                      |
  | ---------- | ------------------------------------ | ---- | -------------------------- |
A
Annie_wang 已提交
510
  | userId | number                               | No  | User ID.<br>Value:<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 已提交
511 512 513 514 515
  | callback   | callback:AsyncCallback&lt;[StorageStats](#StorageStats)&gt; | Yes  | Callback invoked to return the information obtained.|

**Example**

  ```js
A
Annie_wang 已提交
516
  let userId = 1;
A
Annie_wang 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
  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


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


### Attributes
A
Annie_wang 已提交
533 534 535

| Name     | Type  | Description          |
| --------- | ------ | -------------- |
A
Annie_wang 已提交
536 537 538 539 540 541
| total   | number | Total space of the built-in memory card.   |
| audio | number | Space occupied by audio data.  |
| video  | number | Space occupied by video data.|
| image   | number | Space occupied by image data.   |
| file | number | Space occupied by files.  |
| app  | number | Space occupied by application data.|