• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 应用及文件系统空间统计
2
3在系统中,可能出现系统空间不够或者cacheDir等目录受系统配额限制等情况,需要应用开发者关注系统剩余空间,同时控制应用自身占用的空间大小。
4
5## 接口说明
6
7API的详细介绍请参见[ohos.file.statvfs](../reference/apis-core-file-kit/js-apis-file-statvfs.md)、[ohos.file.storageStatistics](../reference/apis-core-file-kit/js-apis-file-storage-statistics.md)。
8
9**表1** 文件系统空间和应用空间统计
10
11| 模块 | 接口名 | 功能 |
12| -------- | -------- | -------- |
13| \@ohos.file.storageStatistics | getCurrentBundleStats | 获取当前应用的存储空间大小(单位为Byte)。 |
14| \@ohos.file.storageStatistics | getFreeSize | 异步获取内置存储的总空间大小(单位为Byte)。 |
15| \@ohos.file.storageStatistics | getFreeSizeSync | 同步获取内置存储的总空间大小(单位为Byte)。 |
16| \@ohos.file.storageStatistics | getTotalSize | 异步获取内置存储的可用空间大小(单位为Byte)。 |
17| \@ohos.file.storageStatistics | getTotalSizeSync | 同步获取内置存储的可用空间大小(单位为Byte)。 |
18| \@ohos.file.statvfs | getFreeSize | 获取指定文件系统的剩余空间大小(单位为Byte)。 |
19| \@ohos.file.statvfs | getTotalSize | 获取指定文件系统的总空间大小(单位为Byte)。 |
20
21**表2** 应用空间统计
22
23| BundleStats属性 | 含义 | 统计路径 |
24| -------- | -------- | -------- |
25| appSize | 应用安装文件大小(单位为Byte) | 应用安装文件保存在以下目录:<br/>/data/storage/el1/bundle |
26| cacheSize | 应用缓存文件大小(单位为Byte) | 应用的缓存文件保存在以下目录:<br/>/data/storage/el1/base/cache<br/>/data/storage/el1/base/haps/entry/cache<br/>/data/storage/el2/base/cache<br/>/data/storage/el2/base/haps/entry/cache |
27| dataSize | 应用文件存储大小(除应用安装文件和缓存文件)(单位为Byte) | 应用文件由本地文件、分布式文件以及数据库文件组成。<br/>本地文件保存在以下目录(注意缓存文件目录为以下目录的子目录):<br/>/data/storage/el1/base<br/>/data/storage/el2/base<br/>分布式文件保存在以下目录:<br/>/data/storage/el2/distributedfiles<br/>数据库文件保存在以下目录:<br/>/data/storage/el1/database<br/>/data/storage/el2/database |
28
29## 开发示例
30
31- 获取文件系统数据分区剩余空间大小。
32
33  ```ts
34  import { statfs } from '@kit.CoreFileKit';
35  import { BusinessError } from '@kit.BasicServicesKit';
36  import { common } from '@kit.AbilityKit';
37
38  // 请在组件内获取context,确保this.getUIContext().getHostContext()返回结果为UIAbilityContext
39  let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
40  let path = context.filesDir;
41  statfs.getFreeSize(path, (err: BusinessError, number: number) => {
42    if (err) {
43      console.error(`Invoke getFreeSize failed, code is ${err.code}, message is ${err.message}`);
44    } else {
45      console.info(`Invoke getFreeSize succeeded, size is ${number}`);
46    }
47  });
48  ```
49
50- 获取当前应用的存储空间大小。
51
52  ```ts
53  import { storageStatistics } from '@kit.CoreFileKit';
54  import { BusinessError } from '@kit.BasicServicesKit';
55
56  storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
57    if (err) {
58      console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
59    } else {
60      console.info(`Invoke getCurrentBundleStats succeeded, appsize is ${bundleStats.appSize}`);
61    }
62  });
63  ```
64
65- 异步获取内置存储的总空间大小。
66
67  ```ts
68  import { storageStatistics } from '@kit.CoreFileKit';
69  import { BusinessError } from '@kit.BasicServicesKit';
70
71  storageStatistics.getTotalSize().then((number: number) => {
72    console.info("getTotalSize successfully:" + JSON.stringify(number));
73  }).catch((err: BusinessError) => {
74    console.error("getTotalSize failed with error:"+ JSON.stringify(err));
75  });
76  ```
77
78- 同步获取内置存储的总空间大小。
79
80  ```ts
81  import { storageStatistics } from '@kit.CoreFileKit';
82  import { BusinessError } from '@kit.BasicServicesKit';
83
84  try {
85    let number = storageStatistics.getTotalSizeSync();
86    console.info("getTotalSizeSync successfully:" + JSON.stringify(number));
87  } catch (err) {
88    let error: BusinessError = err as BusinessError;
89    console.error("getTotalSizeSync failed with error:" + JSON.stringify(error));
90  }
91  ```
92
93- 异步获取内置存储的可用空间大小。
94
95  ```ts
96  import { storageStatistics } from '@kit.CoreFileKit';
97  import { BusinessError } from '@kit.BasicServicesKit';
98
99  storageStatistics.getFreeSize().then((number: number) => {
100    console.info("getFreeSize successfully:" + JSON.stringify(number));
101  }).catch((err: BusinessError) => {
102    console.error("getFreeSize failed with error:" + JSON.stringify(err));
103  });
104  ```
105
106- 同步获取内置存储的可用空间大小。
107
108  ```ts
109  import { storageStatistics } from '@kit.CoreFileKit';
110  import { BusinessError } from '@kit.BasicServicesKit';
111
112  try {
113    let number = storageStatistics.getFreeSizeSync();
114    console.info("getFreeSizeSync successfully:" + JSON.stringify(number));
115  } catch (err) {
116    let error: BusinessError = err as BusinessError;
117    console.error("getFreeSizeSync failed with error:" + JSON.stringify(error));
118  }
119  ```
120
121