• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.storageStatistics (应用空间统计)
2
3该模块提供空间查询相关的常用功能:包括对内外卡的空间查询、对应用分类数据统计的查询、对应用数据的查询等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import  { storageStatistics } from '@kit.CoreFileKit';
13```
14
15## storageStatistics.getCurrentBundleStats<sup>9+</sup>
16
17getCurrentBundleStats(): Promise&lt;BundleStats&gt;
18
19应用异步获取当前应用存储空间大小(单位为Byte),以Promise方式返回。
20
21**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
22
23**返回值:**
24
25  | 类型                                        | 说明                       |
26  | ------------------------------------------ | -------------------------- |
27  | Promise&lt;[Bundlestats](#bundlestats9)&gt; | Promise对象,返回指定卷上的应用存储空间大小(单位为Byte)。      |
28
29**错误码:**
30
31以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
32
33| 错误码ID | 错误信息 |
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**示例:**
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
54应用异步获取当前应用存储空间大小(单位为Byte),以callback方式返回。
55
56**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
57
58**参数:**
59
60  | 参数名    | 类型                                                       | 必填  | 说明                                 |
61  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
62  | callback | AsyncCallback&lt;[BundleStats](#bundlestats9)&gt;          | 是   | 获取指定卷上的应用存储空间大小之后的回调。        |
63
64**错误码:**
65
66以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
67
68| 错误码ID | 错误信息 |
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**示例:**
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
92获取内置存储的总空间大小(单位为Byte),以Promise方式返回。
93
94**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
95
96**返回值:**
97
98| 类型                  | 说明                                                |
99| --------------------- | --------------------------------------------------- |
100| Promise&lt;number&gt; | Promise对象,返回内置存储的总空间大小(单位为Byte)。 |
101
102**错误码:**
103
104以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
105
106| 错误码ID | 错误信息       |
107| -------- | -------------- |
108| 13600001 | IPC error.     |
109| 13900042 | Unknown error. |
110
111**示例:**
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
126获取内置存储的总空间大小(单位为Byte),以callback方式返回。
127
128**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
129
130**参数:**
131
132| 参数名   | 类型                        | 必填 | 说明                               |
133| -------- | --------------------------- | ---- | ---------------------------------- |
134| callback | AsyncCallback&lt;number&gt; | 是   | 获取内置存储的总空间大小之后的回调。 |
135
136**错误码:**
137
138以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
139
140| 错误码ID | 错误信息                                                     |
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**示例:**
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
164同步获取内置存储的总空间大小(单位为Byte)。
165
166**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
167
168**返回值:**
169
170| 类型   | 说明                                   |
171| ------ | -------------------------------------- |
172| number | 返回内置存储的总空间大小(单位为Byte)。 |
173
174**错误码:**
175
176以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
177
178| 错误码ID | 错误信息       |
179| -------- | -------------- |
180| 13600001 | IPC error.     |
181| 13900042 | Unknown error. |
182
183**示例:**
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
200获取内置存储的可用空间大小(单位为Byte),以Promise方式返回。
201
202**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
203
204**返回值:**
205
206| 类型                  | 说明                                                  |
207| --------------------- | ----------------------------------------------------- |
208| Promise&lt;number&gt; | Promise对象,返回内置存储的可用空间大小(单位为Byte)。 |
209
210**错误码:**
211
212以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
213
214| 错误码ID | 错误信息       |
215| -------- | -------------- |
216| 13600001 | IPC error.     |
217| 13900042 | Unknown error. |
218
219**示例:**
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
234获取内置存储的可用空间大小(单位为Byte),以callback方式返回。
235
236**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
237
238**参数:**
239
240| 参数名   | 类型                        | 必填 | 说明                                 |
241| -------- | --------------------------- | ---- | ------------------------------------ |
242| callback | AsyncCallback&lt;number&gt; | 是   | 获取内置存储的可用空间大小之后的回调。 |
243
244**错误码:**
245
246以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
247
248| 错误码ID | 错误信息                                                     |
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**示例:**
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
272同步获取内置存储的可用空间大小(单位为Byte)。
273
274**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
275
276**返回值:**
277
278| 类型   | 说明                                     |
279| ------ | ---------------------------------------- |
280| number | 返回内置存储的可用空间大小(单位为Byte)。 |
281
282**错误码:**
283
284以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
285
286| 错误码ID | 错误信息       |
287| -------- | -------------- |
288| 13600001 | IPC error.     |
289| 13900042 | Unknown error. |
290
291**示例:**
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**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
307
308| 名称      | 类型   | 必填 | 说明           |
309| --------- | ------ | --- | -------------- |
310| appSize   | number | 是 | 应用安装文件大小(单位为Byte)。    |
311| cacheSize | number | 是 | 应用缓存文件大小(单位为Byte)。   |
312| dataSize  | number | 是 | 应用文件存储大小(除应用安装文件和缓存文件)(单位为Byte)。 |
313