• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.storageStatistics (应用空间统计)(系统接口)
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
9该模块提供空间查询相关的常用功能:包括对内外卡的空间查询、对应用分类数据统计的查询、对应用数据的查询等。
10
11> **说明:**
12>
13> - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
14> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.file.storageStatistics (应用空间统计)](js-apis-file-storage-statistics.md)。
15
16## 导入模块
17
18```ts
19import storageStatistics from "@ohos.file.storageStatistics";
20```
21
22## storageStatistics.getTotalSizeOfVolume
23
24getTotalSizeOfVolume(volumeUuid: string): Promise&lt;number&gt;
25
26异步获取外置存储设备中指定卷设备的总空间大小(单位为Byte),以Promise方式返回。
27
28**需要权限**:ohos.permission.STORAGE_MANAGER
29
30**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
31
32**系统接口**:该接口为系统接口。
33
34**参数:**
35
36  | 参数名     | 类型   | 必填 | 说明 |
37  | ---------- | ------ | ---- | ---- |
38  | volumeUuid | string | 是   | 卷设备uuid。 |
39
40**返回值:**
41
42  | 类型                  | 说明             |
43  | --------------------- | ---------------- |
44  | Promise&lt;number&gt; | Promise对象,返回指定卷设备的总空间大小(单位为Byte)。 |
45
46**错误码:**
47
48以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
49
50| 错误码ID | 错误信息 |
51| -------- | -------- |
52| 201 | Permission verification failed. |
53| 202 | The caller is not a system application. |
54| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
55| 13600001 | IPC error. |
56| 13600008 | No such object. |
57| 13900042 | Unknown error. |
58
59**示例:**
60
61  ```ts
62  import volumemanager from "@ohos.file.volumeManager";
63  import { BusinessError } from '@ohos.base';
64
65  volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
66    if (volumes == null || volumes.length <= 0) {
67      console.error("volumes is null or length is invalid");
68      return;
69    }
70    let uuid: string = volumes[0].uuid;
71    storageStatistics.getTotalSizeOfVolume(uuid).then((number: number) => {
72      console.info("getTotalSizeOfVolume successfully:" + number);
73    }).catch((err: BusinessError) => {
74      console.error("getTotalSizeOfVolume failed with error:" + JSON.stringify(err));
75    });
76  }).catch((err: BusinessError) => {
77    console.error("getAllVolumes failed with error:" + JSON.stringify(err));
78  });
79  ```
80
81## storageStatistics.getTotalSizeOfVolume
82
83getTotalSizeOfVolume(volumeUuid: string, callback: AsyncCallback&lt;number&gt;): void
84
85异步获取外置存储设备中指定卷设备的总空间大小(单位为Byte),以callback方式返回。
86
87**需要权限**:ohos.permission.STORAGE_MANAGER
88
89**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
90
91**系统接口**:该接口为系统接口。
92
93**参数:**
94
95  | 参数名     | 类型                                 | 必填 | 说明                       |
96  | ---------- | ------------------------------------ | ---- | -------------------------- |
97  | volumeUuid | string                               | 是   | 卷设备uuid。                       |
98  | callback   | AsyncCallback&lt;number&gt;          | 是   | 获取指定卷设备总空间之后的回调。 |
99
100**错误码:**
101
102以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
103
104| 错误码ID | 错误信息 |
105| -------- | -------- |
106| 201 | Permission verification failed. |
107| 202 | The caller is not a system application. |
108| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
109| 13600001 | IPC error. |
110| 13600008 | No such object. |
111| 13900042 | Unknown error. |
112
113**示例:**
114
115  ```ts
116  import volumemanager from "@ohos.file.volumeManager";
117  import { BusinessError } from '@ohos.base';
118
119  volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
120    if (volumes == null || volumes.length <= 0) {
121      console.error("volumes is null or length is invalid");
122      return;
123    }
124    let uuid: string = volumes[0].uuid;
125    storageStatistics.getTotalSizeOfVolume(uuid, (error: BusinessError, number: number) => {
126      if (error) {
127        console.error("getTotalSizeOfVolume failed with error:" + JSON.stringify(error));
128      } else {
129        // do something
130        console.info("getTotalSizeOfVolume successfully:" + number);
131      }
132    });
133  }).catch((err: BusinessError) => {
134    console.error("getAllVolumes failed with error:" + JSON.stringify(err));
135  });
136  ```
137
138## storageStatistics.getFreeSizeOfVolume
139
140getFreeSizeOfVolume(volumeUuid: string): Promise&lt;number&gt;
141
142异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以Promise方式返回。
143
144**需要权限**:ohos.permission.STORAGE_MANAGER
145
146**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
147
148**系统接口**:该接口为系统接口。
149
150**参数:**
151
152  | 参数名     | 类型   | 必填 | 说明 |
153  | ---------- | ------ | ---- | ---- |
154  | volumeUuid | string | 是   | 卷设备uuid。 |
155
156**返回值:**
157
158  | 类型                  | 说明               |
159  | --------------------- | ------------------ |
160  | Promise&lt;number&gt; | Promise对象,返回指定卷的可用空间大小(单位为Byte)。 |
161
162**错误码:**
163
164以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
165
166| 错误码ID | 错误信息 |
167| -------- | -------- |
168| 201 | Permission verification failed. |
169| 202 | The caller is not a system application. |
170| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
171| 13600001 | IPC error. |
172| 13600008 | No such object. |
173| 13900042 | Unknown error. |
174
175**示例:**
176
177  ```ts
178  import volumemanager from "@ohos.file.volumeManager";
179  import { BusinessError } from '@ohos.base';
180
181  volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
182    if (volumes == null || volumes.length <= 0) {
183      console.error("volumes is null or length is invalid");
184      return;
185    }
186    let uuid: string = volumes[0].uuid;
187    storageStatistics.getFreeSizeOfVolume(uuid).then((number: number) => {
188      console.info("getFreeSizeOfVolume successfully:" + number);
189    }).catch((err: BusinessError) => {
190      console.error("getFreeSizeOfVolume failed with error:" + JSON.stringify(err));
191    });
192  }).catch((err: BusinessError) => {
193    console.error("getAllVolumes failed with error:" + JSON.stringify(err));
194  });
195  ```
196
197## storageStatistics.getFreeSizeOfVolume
198
199getFreeSizeOfVolume(volumeUuid: string, callback: AsyncCallback&lt;number&gt;): void
200
201异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以callback方式返回。
202
203**需要权限**:ohos.permission.STORAGE_MANAGER
204
205**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
206
207**系统接口**:该接口为系统接口。
208
209**参数:**
210
211  | 参数名     | 类型                                 | 必填 | 说明                         |
212  | ---------- | ------------------------------------ | ---- | ---------------------------- |
213  | volumeUuid | string                               | 是   | 卷设备uuid。                         |
214  | callback   | AsyncCallback&lt;number&gt;          | 是   | 获取指定卷可用空间之后的回调。 |
215
216**错误码:**
217
218以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
219
220| 错误码ID | 错误信息 |
221| -------- | -------- |
222| 201 | Permission verification failed. |
223| 202 | The caller is not a system application. |
224| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
225| 13600001 | IPC error. |
226| 13600008 | No such object. |
227| 13900042 | Unknown error. |
228
229**示例:**
230
231  ```ts
232  import volumemanager from "@ohos.file.volumeManager";
233  import { BusinessError } from '@ohos.base';
234
235  volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
236    if (volumes == null || volumes.length <= 0) {
237      console.error("volumes is null or length is invalid");
238      return;
239    }
240    let uuid: string = volumes[0].uuid;
241    storageStatistics.getFreeSizeOfVolume(uuid, (error: BusinessError, number: number) => {
242      if (error) {
243        console.error("getFreeSizeOfVolume failed with error:" + JSON.stringify(error));
244      } else {
245        // do something
246        console.info("getFreeSizeOfVolume successfully: " + number);
247      }
248    });
249  }).catch((err: BusinessError) => {
250    console.error("getAllVolumes failed with error:" + JSON.stringify(err));
251  });
252  ```
253
254## storageStatistics.getBundleStats<sup>9+</sup>
255
256getBundleStats(packageName: string, index?: number): Promise&lt;BundleStats&gt;
257
258异步获取应用存储数据的空间大小(单位为Byte),以Promise方式返回。
259
260**需要权限**:ohos.permission.STORAGE_MANAGER
261
262**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
263
264**系统接口**:该接口为系统接口。
265
266**参数:**
267
268  | 参数名      | 类型   | 必填 | 说明     |
269  | ----------- | ------ | ---- | -------- |
270  | packageName | string | 是   | 应用包名。 |
271  | index<sup>12+</sup> | number | 否   | 分身应用的索引号,默认值为0(表示未分身的主应用)。分身应用索引号在分身创建时默认占用从1开始且当前未被占用的最小索引号,并赋值给该应用的[BundleResourceInfo](../apis-ability-kit/js-apis-bundleManager-BundleResourceInfo-sys.md#bundleresourceinfo)的appIndex属性,后续可以通过调用[getBundleResourceInfo](../apis-ability-kit/js-apis-bundleResourceManager-sys.md#bundleresourcemanagergetbundleresourceinfo12)接口获得。|
272
273**返回值:**
274
275  | 类型                                       | 说明                       |
276  | ------------------------------------------ | -------------------------- |
277  | Promise&lt;[Bundlestats](js-apis-file-storage-statistics.md#bundlestats9)&gt; | Promise对象,返回指定卷上的应用存储数据的空间大小(单位为Byte)。 |
278
279**错误码:**
280
281以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
282
283| 错误码ID | 错误信息 |
284| -------- | -------- |
285| 201 | Permission verification failed. |
286| 202 | The caller is not a system application. |
287| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
288| 13600001 | IPC error. |
289| 13600008 | No such object. |
290| 13900042 | Unknown error. |
291
292**示例:**
293
294  ```ts
295  import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
296  import storageStatistics from "@ohos.file.storageStatistics";
297  import { BusinessError } from '@ohos.base';
298  import { hilog } from '@kit.PerformanceAnalysisKit';
299
300  let bundleName = "com.example.myapplication";
301  let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
302  try {
303    let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags);
304    hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label));
305
306    let packageName:string = bundleName;
307    let index:number = resourceInfo.appIndex;
308    storageStatistics.getBundleStats(packageName, index).then((BundleStats: storageStatistics.BundleStats) => {
309      hilog.info(0x0000, 'testTag', 'getBundleStats successfully. BundleStats: %{public}s', JSON.stringify(BundleStats));
310    }).catch((err: BusinessError) => {
311      hilog.error(0x0000, 'testTag', 'getBundleStats failed with error: %{public}s', JSON.stringify(err));
312    });
313
314  } catch (err) {
315    let message = (err as BusinessError).message;
316    hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed with error: %{public}s', message);
317  }
318  ```
319
320## storageStatistics.getBundleStats<sup>9+</sup>
321
322getBundleStats(packageName: string,  callback: AsyncCallback&lt;BundleStats&gt;, index?: number): void
323
324异步获取应用存储数据的空间大小(单位为Byte),以callback方式返回。
325
326**需要权限**:ohos.permission.STORAGE_MANAGER
327
328**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
329
330**系统接口**:该接口为系统接口。
331
332**参数:**
333
334  | 参数名   | 类型                                                      | 必填 | 说明                                 |
335  | -------- | --------------------------------------------------------- | ---- | ------------------------------------ |
336  | packageName | string | 是   | 应用包名。 |
337  | callback | AsyncCallback&lt;[Bundlestats](js-apis-file-storage-statistics.md#bundlestats9)&gt; | 是   | 获取指定卷上的应用存储数据的空间大小之后的回调。 |
338  | index<sup>12+</sup> | number | 否   | 分身应用的索引号,默认值为0(表示未分身的主应用)。分身应用索引号在分身创建时默认占用从1开始且当前未被占用的最小索引号,并赋值给该应用的[BundleResourceInfo](../apis-ability-kit/js-apis-bundleManager-BundleResourceInfo-sys.md#bundleresourceinfo)的appIndex属性,后续可以通过调用[getBundleResourceInfo](../apis-ability-kit/js-apis-bundleResourceManager-sys.md#bundleresourcemanagergetbundleresourceinfo12)接口获得。|
339
340**错误码:**
341
342以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
343
344| 错误码ID | 错误信息 |
345| -------- | -------- |
346| 201 | Permission verification failed. |
347| 202 | The caller is not a system application. |
348| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
349| 13600001 | IPC error. |
350| 13600008 | No such object. |
351| 13900042 | Unknown error. |
352
353**示例:**
354
355  ```ts
356  import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
357  import storageStatistics from "@ohos.file.storageStatistics";
358  import { BusinessError } from '@ohos.base';
359  import { hilog } from '@kit.PerformanceAnalysisKit';
360
361  let bundleName = "com.example.myapplication";
362  let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
363  try {
364    let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags);
365    hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label));
366
367    let packageName:string = bundleName;
368    let index:number = resourceInfo.appIndex;
369    storageStatistics.getBundleStats(packageName, (err: BusinessError, BundleStats: storageStatistics.BundleStats) => {
370      if (err) {
371        hilog.error(0x0000, 'testTag', 'getBundleStats failed with error: %{public}s', JSON.stringify(err));
372      } else {
373        hilog.info(0x0000, 'testTag', 'getBundleStats successfully. BundleStats: %{public}s', JSON.stringify(BundleStats));
374      }
375    }, index);
376
377  } catch (err) {
378    let message = (err as BusinessError).message;
379    hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed: %{public}s', message);
380  }
381  ```
382
383## storageStatistics.getSystemSize<sup>9+</sup>
384
385getSystemSize(): Promise&lt;number&gt;
386
387异步获取系统数据的空间大小(单位为Byte),以Promise方式返回。
388
389**需要权限**:ohos.permission.STORAGE_MANAGER
390
391**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
392
393**系统接口**:该接口为系统接口。
394
395**返回值:**
396
397  | 类型                  | 说明             |
398  | --------------------- | ---------------- |
399  | Promise&lt;number&gt; | Promise对象,返回系统数据的空间大小(单位为Byte)。 |
400
401**错误码:**
402
403以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
404
405| 错误码ID | 错误信息 |
406| -------- | -------- |
407| 201 | Permission verification failed. |
408| 202 | The caller is not a system application. |
409| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. |
410| 13600001 | IPC error. |
411| 13900042 | Unknown error. |
412
413**示例:**
414
415  ```ts
416  import { BusinessError } from '@ohos.base';
417  storageStatistics.getSystemSize().then((number: number) => {
418    console.info("getSystemSize successfully:" + number);
419  }).catch((err: BusinessError) => {
420    console.error("getSystemSize failed with error:" + JSON.stringify(err));
421  });
422  ```
423
424## storageStatistics.getSystemSize<sup>9+</sup>
425
426getSystemSize(callback: AsyncCallback&lt;number&gt;): void
427
428异步获取系统数据的空间大小(单位为Byte),以callback方式返回。
429
430**需要权限**:ohos.permission.STORAGE_MANAGER
431
432**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
433
434**系统接口**:该接口为系统接口。
435
436**参数:**
437
438  | 参数名     | 类型                                 | 必填 | 说明                       |
439  | ---------- | ------------------------------------ | ---- | -------------------------- |
440  | callback   |  AsyncCallback&lt;number&gt;         | 是   | 获取系统数据的空间大小之后的回调。 |
441
442**错误码:**
443
444以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
445
446| 错误码ID | 错误信息 |
447| -------- | -------- |
448| 201 | Permission verification failed. |
449| 202 | The caller is not a system application. |
450| 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. |
451| 13600001 | IPC error. |
452| 13900042 | Unknown error. |
453
454**示例:**
455
456  ```ts
457  import { BusinessError } from '@ohos.base';
458  storageStatistics.getSystemSize((error: BusinessError, number: number) => {
459    if (error) {
460      console.error("getSystemSize failed with error:" + JSON.stringify(error));
461    } else {
462      // do something
463      console.info("getSystemSize successfully:" + number);
464    }
465  });
466  ```
467
468## storageStatistics.getUserStorageStats<sup>9+</sup>
469
470getUserStorageStats(): Promise&lt;StorageStats&gt;
471
472异步获取当前用户各类别存储空间大小(单位为Byte),以Promise方式返回。
473
474**需要权限**:ohos.permission.STORAGE_MANAGER
475
476**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
477
478**系统接口**:该接口为系统接口。
479
480**返回值:**
481
482  | 类型                  | 说明             |
483  | --------------------- | ---------------- |
484  | Promise&lt;[StorageStats](#storagestats9)&gt; | Promise对象,返回当前用户各类别存储空间大小(单位为Byte)。 |
485
486**错误码:**
487
488以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
489
490| 错误码ID | 错误信息 |
491| -------- | -------- |
492| 201 | Permission verification failed. |
493| 202 | The caller is not a system application. |
494| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
495| 13600001 | IPC error. |
496| 13900042 | Unknown error. |
497
498**示例:**
499
500  ```ts
501  import { BusinessError } from '@ohos.base';
502  storageStatistics.getUserStorageStats().then((storageStats: storageStatistics.StorageStats) => {
503    console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats));
504  }).catch((err: BusinessError) => {
505    console.error("getUserStorageStats failed with error:" + JSON.stringify(err));
506  });
507  ```
508
509## storageStatistics.getUserStorageStats<sup>9+</sup>
510
511getUserStorageStats(callback: AsyncCallback&lt;StorageStats&gt;): void
512
513异步获取当前用户各类别存储空间大小(单位为Byte),以callback方式返回。
514
515**需要权限**:ohos.permission.STORAGE_MANAGER
516
517**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
518
519**系统接口**:该接口为系统接口。
520
521**参数:**
522
523  | 参数名     | 类型                                 | 必填 | 说明                       |
524  | ---------- | ------------------------------------ | ---- | -------------------------- |
525  | callback   | AsyncCallback&lt;[StorageStats](#storagestats9)&gt; | 是   | 返回用户各类别存储空间大小之后的回调。 |
526
527**错误码:**
528
529以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
530
531| 错误码ID | 错误信息 |
532| -------- | -------- |
533| 201 | Permission verification failed. |
534| 202 | The caller is not a system application. |
535| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
536| 13600001 | IPC error. |
537| 13900042 | Unknown error. |
538
539**示例:**
540
541  ```ts
542  import { BusinessError } from '@ohos.base';
543  storageStatistics.getUserStorageStats((error: BusinessError, storageStats: storageStatistics.StorageStats) => {
544    if (error) {
545      console.error("getUserStorageStats failed with error:" + JSON.stringify(error));
546    } else {
547      // do something
548      console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats));
549    }
550  });
551  ```
552
553## storageStatistics.getUserStorageStats<sup>9+</sup>
554
555getUserStorageStats(userId: number): Promise&lt;StorageStats&gt;
556
557异步获取指定用户各类别存储空间大小(单位为Byte),以Promise方式返回。
558
559**需要权限**:ohos.permission.STORAGE_MANAGER
560
561**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
562
563**系统接口**:该接口为系统接口。
564
565**参数:**
566
567  | 参数名     | 类型   | 必填 | 说明 |
568  | ---------- | ------ | ---- | ---- |
569  | userId | number | 是   | 用户id。|
570
571**返回值:**
572
573  | 类型                  | 说明             |
574  | --------------------- | ---------------- |
575  | Promise&lt;[StorageStats](#storagestats9)&gt; | Promise对象,返回指定用户各类别存储空间大小(单位为Byte)。 |
576
577**错误码:**
578
579以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
580
581| 错误码ID | 错误信息 |
582| -------- | -------- |
583| 201 | Permission verification failed. |
584| 202 | The caller is not a system application. |
585| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
586| 13600001 | IPC error. |
587| 13600009 | User if out of range. |
588| 13900042 | Unknown error. |
589
590**示例:**
591
592  ```ts
593  import { BusinessError } from '@ohos.base';
594  let userId: number = 100;
595  storageStatistics.getUserStorageStats(userId).then((storageStats: storageStatistics.StorageStats) => {
596    console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats));
597  }).catch((err: BusinessError) => {
598    console.error("getUserStorageStats failed with error:" + JSON.stringify(err));
599  });
600  ```
601
602## storageStatistics.getUserStorageStats<sup>9+</sup>
603
604getUserStorageStats(userId: number, callback: AsyncCallback&lt;StorageStats&gt;): void
605
606异步获取指定用户各类别存储空间大小(单位为Byte),以callback方式返回。
607
608**需要权限**:ohos.permission.STORAGE_MANAGER
609
610**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
611
612**系统接口**:该接口为系统接口。
613
614**参数:**
615
616  | 参数名     | 类型                                 | 必填 | 说明                       |
617  | ---------- | ------------------------------------ | ---- | -------------------------- |
618  | userId | number                               | 是   | 用户id。 |
619  | callback   | AsyncCallback&lt;[StorageStats](#storagestats9)&gt; | 是   | 返回指定用户各类别存储空间大小之后的回调。 |
620
621**错误码:**
622
623以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。
624
625| 错误码ID | 错误信息 |
626| -------- | -------- |
627| 201 | Permission verification failed. |
628| 202 | The caller is not a system application. |
629| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
630| 13600001 | IPC error. |
631| 13600009 | User if out of range. |
632| 13900042 | Unknown error. |
633
634**示例:**
635
636  ```ts
637  import { BusinessError } from '@ohos.base';
638  let userId: number = 100;
639  storageStatistics.getUserStorageStats(userId, (error: BusinessError, storageStats: storageStatistics.StorageStats) => {
640    if (error) {
641      console.error("getUserStorageStats failed with error:" + JSON.stringify(error));
642    } else {
643      // do something
644      console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats));
645    }
646  });
647  ```
648
649## StorageStats<sup>9+</sup>
650
651**系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics
652
653**系统接口**:该接口为系统接口。
654
655| 名称      | 类型   | 只读  | 可选  | 说明           |
656| --------- | ------ | ---- | ----- | -------------- |
657| total   | number | 否 | 否 | 内置存储总空间大小,单位为Byte。    |
658| audio | number  |否 | 否 | 音频数据大小,单位为Byte。  |
659| video  | number | 否 | 否 | 视频数据大小,单位为Byte。 |
660| image   | number | 否 | 否 | 图像数据大小,单位为Byte。   |
661| file | number | 否 | 否 | 文件数据大小,单位为Byte。  |
662| app  | number | 否 | 否 | 应用数据大小,单位为Byte。 |
663