# 应用空间统计 > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** > > - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > - API 9当前为Canary版本,仅供试用,不保证接口可稳定调用。 该模块提供空间查询相关的常用功能:包括对内外卡的空间查询,对应用分类数据统计的查询,对应用数据的查询等。 ## 导入模块 ```js import storageStatistics from "@ohos.storageStatistics"; ``` ## storagestatistics.getTotalSizeOfVolume getTotalSizeOfVolume(volumeUuid: string): Promise<number> 异步获取指定卷的总空间大小,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | volumeUuid | string | 是 | 卷id | - 返回值 | 类型 | 说明 | | --------------------- | ---------------- | | Promise<number> | 返回指定卷总空间 | - 示例 ```js let uuid = ""; storagestatistics.getTotalSizeOfVolume(uuid).then(function(number){ console.info("getTotalSizeOfVolume successfully:"+ number); }).catch(function(err){ console.info("getTotalSizeOfVolume failed with error:"+ err); }); ``` ## storagestatistics.getTotalSizeOfVolume getTotalSizeOfVolume(volumeUuid: string, callback:AsyncCallback<number>):void 异步获取指定卷的总空间大小,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | volumeUuid | string | 是 | 卷id | | callback | callback:AsyncCallback<number> | 是 | 获取指定卷总空间之后的回调 | - 示例 ```js let uuid = ""; storagestatistics.getTotalSizeOfVolume(uuid, function(error, number){ // do something console.info("getTotalSizeOfVolume successfully:"+ number); }); ``` ## storagestatistics.getFreeSizeOfVolume getFreeSizeOfVolume(volumeUuid: string): Promise<number> 异步获取指定卷的可用空间大小,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | volumeUuid | string | 是 | 卷id | - 返回值 | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | 返回指定卷可用空间 | - 示例 ```js let uuid = ""; storagestatistics.getFreeSizeOfVolume(uuid).then(function(number){ console.info("getFreeSizeOfVolume successfully:"+ number); }).catch(function(err){ console.info("getFreeSizeOfVolume failed with error:"+ err); }); ``` ## storagestatistics.getFreeSizeOfVolume getFreeSizeOfVolume(volumeUuid: string, callback:AsyncCallback<number>):void 异步获取指定卷的可用空间大小,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | ---------------------------- | | volumeUuid | string | 是 | 卷id | | callback | callback:AsyncCallback<number> | 是 | 获取指定卷可用空间之后的回调 | - 示例 ```js let uuid = ""; storagestatistics.getFreeSizeOfVolume(uuid, function(error, number){ // do something console.info("getFreeSizeOfVolume successfully:"+ number); }); ``` ## storagestatistics.getBundleStats9+ getBundleStats(packageName: string): Promise<BundleStats> 异步获取应用存储数据,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | -------- | | packageName | string | 是 | 应用包名 | - 返回值 | 类型 | 说明 | | ------------------------------------------ | -------------------------- | | Promise<[Bundlestats](#bundlestats)> | 返回指定卷上的应用存储数据 | - 示例 ```js let packageName = ""; storagestatistics.getBundleStats(packageName).then(function(BundleStats){ console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats)); }).catch(function(err){ console.info("getBundleStats failed with error:"+ err); }); ``` ## storagestatistics.getBundleStats9+ getBundleStats(packageName: string, callback: AsyncCallback<BundleStats>): void 异步获取应用存储数据,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | | packageName | string | 是 | 应用包名 | | callback | callback:AsyncCallback<[Bundlestats](#bundlestats)> | 是 | 获取指定卷上的应用存储数据之后的回调 | - 示例 ```js let packageName = ""; storagestatistics.getBundleStats(packageName, function(error, BundleStats){ // do something console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats)); }); ``` ## storagestatistics.getCurrentBundleStats9+ getCurrentBundleStats(): Promise 第三方应用异步获取占用空间,以promise方式返回。 **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics - 返回值 | 类型 | 说明 | | ------------------------------------------ | -------------------------- | | Promise<[Bundlestats](#bundlestats)> | 返回指定卷上的应用存储状态 | - 示例 ```js let bundleStats = storageStatistics.getCurrentBundleStats(); console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats)); ``` ## storagestatistics.getCurrentBundleStats9+ getCurrentBundleStats(callback: AsyncCallback): void 第三方应用异步获取占用空间,以callback方式返回。 **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics - 参数 | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | | callback | callback:AsyncCallback<[BundleStats](#bundlestats)> | 是 | 获取指定卷上的应用存储状态之后的回调 | - 示例 ```js storagestatistics.getCurrentBundleStats(function(error, bundleStats){ // do something console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats)); }); ``` ## BundleStats9+ **系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.StorageService.SpatialStatistics。 **系统API**:该接口为系统接口,三方应用不支持调用 - 属性 | 名称 | 类型 | 说明 | | --------- | ------ | -------------- | | appSize | number | app数据大小 | | cacheSize | number | 缓存数据大小 | | dataSize | number | 应用总数据大小 | ## storagestatistics.getTotalSize9+ getTotalSize(): Promise 获取内卡的总空间大小,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 返回值 | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | 返回内卡的总空间大小 | - 示例 ```js let number = storageStatistics.getTotalSize(); console.info("getTotalSize successfully:"+ JSON.stringify(number)); ``` ## storagestatistics.getTotalSize9+ getTotalSize(callback: AsyncCallback): void 获取内卡的总空间大小,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------ | ---- | ------------------------ | | callback | callback:AsyncCallback<number> | 是 | 获取内卡的总空间大小之后的回调 | - 示例 ```js storagestatistics.getTotalSize(function(error, number){ // do something console.info("getTotalSize successfully:"+ JSON.stringify(number)); }); ``` ## storagestatistics.getFreeSize9+ getFreeSize(): Promise 获取内卡的可用空间大小,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 返回值 | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | 返回内卡的可用空间大小 | - 示例 ```js let number = storageStatistics.getFreeSize(); console.info("getFreeSize successfully:"+ JSON.stringify(number)); ``` ## storagestatistics.getFreeSize9+ getFreeSize(callback: AsyncCallback): void 获取内卡的可用空间大小,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------ | ---- | ------------------------- | | callback | callback:AsyncCallback<number> | 是 | 获取内卡的可用空间大小之后的回调 | - 示例 ```js storagestatistics.getFreeSize(function(error, number){ // do something console.info("getFreeSize successfully:"+ JSON.stringify(number)); }); ``` ## storagestatistics.getSystemSize9+ getSystemSize(): Promise<number> 异步获取系统空间大小,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 返回值 | 类型 | 说明 | | --------------------- | ---------------- | | Promise<number> | 返回系统空间大小 | - 示例 ```js storagestatistics.getSystemSize().then(function(number){ console.info("getSystemSize successfully:"+ number); }).catch(function(err){ console.info("getSystemSize failed with error:"+ err); }); ``` ## storagestatistics.getSystemSize9+ getSystemSize(callback:AsyncCallback<number>):void 异步获取系统空间大小,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | callback | callback:AsyncCallback<number> | 是 | 获取系统空间大小之后的回调 | - 示例 ```js storagestatistics.getSystemSize(function(error, number){ // do something console.info("getSystemSize successfully:"+ number); }); ``` ## storagestatistics.getUserStorageStats9+ getUserStorageStats(userId?: number): Promise<StorageStats> 异步获取用户各类别数据大小,以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | userId | string | 否 | 用户id
确认当前用户:
- 有值:表示指定用户。
- 无值:表示当前用户。| - 返回值 | 类型 | 说明 | | --------------------- | ---------------- | | Promise<[StorageStats](#StorageStats)> | 返回各类别数据大小 | - 示例 ```js let userId = ""; storagestatistics.getUserStorageStats(userId).then(function(StorageStats){ console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); }).catch(function(err){ console.info("getUserStorageStats failed with error:"+ err); }); ``` ## storagestatistics.getUserStorageStats9+ getUserStorageStats(userId?: number, callback:AsyncCallback<StorageStats>):void 异步获取用户各类别数据大小,以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统API**:该接口为系统接口,三方应用不支持调用 - 参数 | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | userId | string | 否 | 用户id
确认当前用户:
- 有值:表示指定用户。
- 无值:表示当前用户。 | | callback | callback:AsyncCallback<[StorageStats](#StorageStats)> | 是 | 返回各类别数据大小之后的回调 | - 示例 ```js let userId = ""; storagestatistics.getUserStorageStats(userId, function(error, StorageStats){ // do something console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); }); ``` ## StorageStats9+ **系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.StorageService.SpatialStatistics。 **系统API**:该接口为系统接口,三方应用不支持调用 - 属性 | 名称 | 类型 | 说明 | | --------- | ------ | -------------- | | total | number | 内卡总空间大小 | | audio | number | 音频数据大小 | | video | number | 视频数据大小 | | image | number | 图像数据大小 | | file | number | 文件数据大小 | | app | number | 应用数据大小 |