• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.storageStatistics (Application Storage Statistics)
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
9The **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.
10
11> **NOTE**
12>
13> 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.
14
15## Modules to Import
16
17```ts
18import  { storageStatistics } from '@kit.CoreFileKit';
19```
20
21## storageStatistics.getCurrentBundleStats<sup>9+</sup>
22
23getCurrentBundleStats(): Promise&lt;BundleStats&gt;
24
25Obtains the storage space (in bytes) of this application. This API uses a promise to return the result.
26
27**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
28
29**Return value**
30
31  | Type                                       | Description                      |
32  | ------------------------------------------ | -------------------------- |
33  | Promise&lt;[BundleStats](#bundlestats9)&gt; | Promise used to return the application storage space obtained.     |
34
35**Error codes**
36
37For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
38
39| ID| Error Message|
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**Example**
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&lt;BundleStats&gt;): void
59
60Obtains the storage space (in bytes) of this application. This API uses a promise to return the result.
61
62**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
63
64**Parameters**
65
66  | Name   | Type                                                      | Mandatory | Description                                |
67  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
68  | callback | AsyncCallback&lt;[BundleStats](#bundlestats9)&gt;          | Yes  | Callback used to return the application space obtained.       |
69
70**Error codes**
71
72For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
73
74| ID| Error Message|
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**Example**
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&lt;number&gt;
97
98Obtains the total space of the built-in storage, in bytes. This API uses a promise to return the result.
99
100**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
101
102**Return value**
103
104| Type                 | Description                                               |
105| --------------------- | --------------------------------------------------- |
106| Promise&lt;number&gt; | Promise used to return the total built-in storage space obtained.|
107
108**Error codes**
109
110For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
111
112| ID| Error Message      |
113| -------- | -------------- |
114| 13600001 | IPC error.     |
115| 13900042 | Unknown error. |
116
117**Example**
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&lt;number&gt;): void
131
132Obtains the total space of the built-in storage, in bytes. This API uses an asynchronous callback to return the result.
133
134**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
135
136**Parameters**
137
138| Name  | Type                       | Mandatory| Description                              |
139| -------- | --------------------------- | ---- | ---------------------------------- |
140| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the built-in storage space obtained.|
141
142**Error codes**
143
144For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
145
146| ID| Error Message                                                    |
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**Example**
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
170Obtains the total space of the built-in storage, in bytes. This API returns the result synchronously.
171
172**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
173
174**Return value**
175
176| Type  | Description                                  |
177| ------ | -------------------------------------- |
178| number | Built-in storage space obtained.|
179
180**Error codes**
181
182For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
183
184| ID| Error Message      |
185| -------- | -------------- |
186| 13600001 | IPC error.     |
187| 13900042 | Unknown error. |
188
189**Example**
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&lt;number&gt;
205
206Obtains the available space of the built-in storage, in bytes. This API uses a promise to return the result.
207
208**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
209
210**Return value**
211
212| Type                 | Description                                                 |
213| --------------------- | ----------------------------------------------------- |
214| Promise&lt;number&gt; | Promise used to return the available space of the built-in storage obtained.|
215
216**Error codes**
217
218For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
219
220| ID| Error Message      |
221| -------- | -------------- |
222| 13600001 | IPC error.     |
223| 13900042 | Unknown error. |
224
225**Example**
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&lt;number&gt;): void
239
240Obtains the available space of the built-in storage, in bytes. This API uses an asynchronous callback to return the result.
241
242**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
243
244**Parameters**
245
246| Name  | Type                       | Mandatory| Description                                |
247| -------- | --------------------------- | ---- | ------------------------------------ |
248| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the available space of the built-in storage obtained.|
249
250**Error codes**
251
252For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md) and [Universal Error Codes](../errorcode-universal.md).
253
254| ID| Error Message                                                    |
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**Example**
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
278Obtains the available space of the built-in storage, in bytes. This API returns the result synchronously.
279
280**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
281
282**Return value**
283
284| Type  | Description                                    |
285| ------ | ---------------------------------------- |
286| number | Available space of the built-in storage obtained.|
287
288**Error codes**
289
290For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
291
292| ID| Error Message      |
293| -------- | -------------- |
294| 13600001 | IPC error.     |
295| 13900042 | Unknown error. |
296
297**Example**
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**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
313
314| Name     | Type  | Mandatory| Description          |
315| --------- | ------ | --- | -------------- |
316| appSize   | number | Yes| Size of the application installation files, in bytes.   |
317| cacheSize | number | Yes| Size of the application cache files, in bytes.  |
318| dataSize  | number | Yes| Size of application files (excluding application installation files), in bytes.|
319