• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.storageStatistics (Application Storage Statistics)
2
3The **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.
4
5> **NOTE**
6>
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.
8
9## Modules to Import
10
11```ts
12import  { storageStatistics } from '@kit.CoreFileKit';
13```
14
15## storageStatistics.getCurrentBundleStats<sup>9+</sup>
16
17getCurrentBundleStats(): Promise&lt;BundleStats&gt;
18
19Obtains the storage space (in bytes) of this application. This API uses a promise to return the result.
20
21**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
22
23**Return value**
24
25  | Type                                       | Description                      |
26  | ------------------------------------------ | -------------------------- |
27  | Promise&lt;[Bundlestats](#bundlestats9)&gt; | Promise used to return the application storage space obtained.     |
28
29**Error codes**
30
31For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
32
33| ID| Error Message|
34| -------- | -------- |
35| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. |
36| 13600001 | IPC error. |
37| 13900042 | Unknown error. |
38
39**Example**
40
41  ```ts
42  import { BusinessError } from '@kit.BasicServicesKit';
43  storageStatistics.getCurrentBundleStats().then((BundleStats: storageStatistics.BundleStats) => {
44    console.info("getCurrentBundleStats successfully:" + JSON.stringify(BundleStats));
45  }).catch((err: BusinessError) => {
46    console.error("getCurrentBundleStats failed with error:"+ JSON.stringify(err));
47  });
48  ```
49
50## storageStatistics.getCurrentBundleStats<sup>9+</sup>
51
52getCurrentBundleStats(callback: AsyncCallback&lt;BundleStats&gt;): void
53
54Obtains the storage space (in bytes) of this application. This API uses a promise to return the result.
55
56**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
57
58**Parameters**
59
60  | Name   | Type                                                      | Mandatory | Description                                |
61  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
62  | callback | AsyncCallback&lt;[BundleStats](#bundlestats9)&gt;          | Yes  | Callback used to return the application space obtained.       |
63
64**Error codes**
65
66For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
67
68| ID| Error Message|
69| -------- | -------- |
70| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. |
71| 13600001 | IPC error. |
72| 13900042 | Unknown error. |
73
74**Example**
75
76  ```ts
77  import { BusinessError } from '@kit.BasicServicesKit';
78  storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
79    if (error) {
80      console.error("getCurrentBundleStats failed with error:" + JSON.stringify(error));
81    } else {
82      // Do something.
83      console.info("getCurrentBundleStats successfully:" + JSON.stringify(bundleStats));
84    }
85  });
86  ```
87
88## storageStatistics.getTotalSize<sup>15+</sup>
89
90getTotalSize(): Promise&lt;number&gt;
91
92Obtains the total space of the built-in storage, in bytes. This API uses a promise to return the result.
93
94**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
95
96**Return value**
97
98| Type                 | Description                                               |
99| --------------------- | --------------------------------------------------- |
100| Promise&lt;number&gt; | Promise used to return the total built-in storage space obtained.|
101
102**Error codes**
103
104For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
105
106| ID| Error Message      |
107| -------- | -------------- |
108| 13600001 | IPC error.     |
109| 13900042 | Unknown error. |
110
111**Example**
112
113  ```ts
114import { BusinessError } from '@kit.BasicServicesKit';
115storageStatistics.getTotalSize().then((number: number) => {
116  console.info("getTotalSize successfully:" + JSON.stringify(number));
117}).catch((err: BusinessError) => {
118  console.error("getTotalSize failed with error:"+ JSON.stringify(err));
119});
120  ```
121
122## storageStatistics.getTotalSize<sup>15+</sup>
123
124getTotalSize(callback: AsyncCallback&lt;number&gt;): void
125
126Obtains the total space of the built-in storage, in bytes. This API uses an asynchronous callback to return the result.
127
128**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
129
130**Parameters**
131
132| Name  | Type                       | Mandatory| Description                              |
133| -------- | --------------------------- | ---- | ---------------------------------- |
134| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the built-in storage space obtained.|
135
136**Error codes**
137
138For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
139
140| ID| Error Message                                                    |
141| -------- | ------------------------------------------------------------ |
142| 401      | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. |
143| 13600001 | IPC error.                                                   |
144| 13900042 | Unknown error.                                               |
145
146**Example**
147
148  ```ts
149import { BusinessError } from '@kit.BasicServicesKit';
150storageStatistics.getTotalSize((error: BusinessError, number: number) => {
151  if (error) {
152    console.error("getTotalSize failed with error:" + JSON.stringify(error));
153  } else {
154    // Do something.
155    console.info("getTotalSize successfully:" + number);
156  }
157});
158  ```
159
160## storageStatistics.getTotalSizeSync<sup>15+</sup>
161
162getTotalSizeSync(): number
163
164Obtains the total space of the built-in storage, in bytes. This API returns the result synchronously.
165
166**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
167
168**Return value**
169
170| Type  | Description                                  |
171| ------ | -------------------------------------- |
172| number | Built-in storage space obtained.|
173
174**Error codes**
175
176For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
177
178| ID| Error Message      |
179| -------- | -------------- |
180| 13600001 | IPC error.     |
181| 13900042 | Unknown error. |
182
183**Example**
184
185  ```ts
186import { BusinessError } from '@kit.BasicServicesKit';
187try {
188  let number = storageStatistics.getTotalSizeSync();
189  console.info("getTotalSizeSync successfully:" + JSON.stringify(number));
190} catch (err) {
191  let error: BusinessError = err as BusinessError;
192  console.error("getTotalSizeSync failed with error:" + JSON.stringify(error));
193}
194  ```
195
196## storageStatistics.getFreeSize<sup>15+</sup>
197
198getFreeSize(): Promise&lt;number&gt;
199
200Obtains the available space of the built-in storage, in bytes. This API uses a promise to return the result.
201
202**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
203
204**Return value**
205
206| Type                 | Description                                                 |
207| --------------------- | ----------------------------------------------------- |
208| Promise&lt;number&gt; | Promise used to return the available space of the built-in storage obtained.|
209
210**Error codes**
211
212For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
213
214| ID| Error Message      |
215| -------- | -------------- |
216| 13600001 | IPC error.     |
217| 13900042 | Unknown error. |
218
219**Example**
220
221  ```ts
222import { BusinessError } from '@kit.BasicServicesKit';
223storageStatistics.getFreeSize().then((number: number) => {
224  console.info("getFreeSize successfully:" + JSON.stringify(number));
225}).catch((err: BusinessError) => {
226  console.error("getFreeSize failed with error:" + JSON.stringify(err));
227});
228  ```
229
230## storageStatistics.getFreeSize<sup>15+</sup>
231
232getFreeSize(callback: AsyncCallback&lt;number&gt;): void
233
234Obtains the available space of the built-in storage, in bytes. This API uses an asynchronous callback to return the result.
235
236**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
237
238**Parameters**
239
240| Name  | Type                       | Mandatory| Description                                |
241| -------- | --------------------------- | ---- | ------------------------------------ |
242| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the available space of the built-in storage obtained.|
243
244**Error codes**
245
246For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
247
248| ID| Error Message                                                    |
249| -------- | ------------------------------------------------------------ |
250| 401      | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. |
251| 13600001 | IPC error.                                                   |
252| 13900042 | Unknown error.                                               |
253
254**Example**
255
256  ```ts
257import { BusinessError } from '@kit.BasicServicesKit';
258storageStatistics.getFreeSize((error: BusinessError, number: number) => {
259  if (error) {
260    console.error("getFreeSize failed with error:" + JSON.stringify(error));
261  } else {
262    // Do something.
263    console.info("getFreeSize successfully:" + number);
264  }
265});
266  ```
267
268## storageStatistics.getFreeSizeSync<sup>15+</sup>
269
270getFreeSizeSync(): number
271
272Obtains the available space of the built-in storage, in bytes. This API returns the result synchronously.
273
274**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
275
276**Return value**
277
278| Type  | Description                                    |
279| ------ | ---------------------------------------- |
280| number | Available space of the built-in storage obtained.|
281
282**Error codes**
283
284For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
285
286| ID| Error Message      |
287| -------- | -------------- |
288| 13600001 | IPC error.     |
289| 13900042 | Unknown error. |
290
291**Example**
292
293  ```ts
294import { BusinessError } from '@kit.BasicServicesKit';
295try {
296  let number = storageStatistics.getFreeSizeSync();
297  console.info("getFreeSizeSync successfully:" + JSON.stringify(number));
298} catch (err) {
299  let error: BusinessError = err as BusinessError;
300  console.error("getFreeSizeSync failed with error:" + JSON.stringify(error));
301}
302  ```
303
304## BundleStats<sup>9+</sup>
305
306**System capability**: SystemCapability.FileManagement.StorageService.SpatialStatistics
307
308| Name     | Type  | Mandatory| Description          |
309| --------- | ------ | --- | -------------- |
310| appSize   | number | Yes| Size of the application installation files, in bytes.   |
311| cacheSize | number | Yes| Size of the application cache files, in bytes.  |
312| dataSize  | number | Yes| Size of other files of the application, in bytes.|
313