# @ohos.file.storageStatistics (应用空间统计) 该模块提供空间查询相关的常用功能:包括对内外卡的空间查询,对应用分类数据统计的查询,对应用数据的查询等。 > **说明:** > > - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > - 本模块支持对错误码进行处理,错误码及其适配方式[参考文档](../errorcodes/errorcode-filemanagement.md#错误码适配指导)。 ## 导入模块 ```js import storageStatistics from "@ohos.file.storageStatistics"; ``` ## storageStatistics.getTotalSizeOfVolume getTotalSizeOfVolume(volumeUuid: string): Promise<number> 异步获取外置存储设备中指定卷设备的总空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | volumeUuid | string | 是 | 卷设备uuid | **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<number> | 返回指定卷设备的总空间大小(单位为Byte) | **示例:** ```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 异步获取外置存储设备中指定卷设备的总空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | volumeUuid | string | 是 | 卷设备uuid | | 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> 异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | volumeUuid | string | 是 | 卷设备uuid | **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | 返回指定卷的可用空间大小(单位为Byte) | **示例:** ```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 异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | ---------------------------- | | volumeUuid | string | 是 | 卷设备uuid | | 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> 异步获取应用存储空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | -------- | | packageName | string | 是 | 应用包名 | **返回值:** | 类型 | 说明 | | ------------------------------------------ | -------------------------- | | Promise<[Bundlestats](#bundlestats9)> | 返回指定卷上的应用存储数据(单位为Byte) | **示例:** ```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 异步获取应用存储空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | | packageName | string | 是 | 应用包名 | | callback | AsyncCallback<[Bundlestats](#bundlestats9)> | 是 | 获取指定卷上的应用存储数据之后的回调 | **示例:** ```js let packageName = ""; storageStatistics.getBundleStats(packageName, function(error, BundleStats){ // do something console.info("getBundleStats successfully:"+ JSON.stringify(BundleStats)); }); ``` ## storageStatistics.getCurrentBundleStats9+ getCurrentBundleStats(): Promise<BundleStats> 第三方应用异步获取当前应用存储空间大小(单位为Byte),以promise方式返回。 **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **返回值:** | 类型 | 说明 | | ------------------------------------------ | -------------------------- | | Promise<[Bundlestats](#bundlestats9)> | 返回指定卷上的应用存空间大小(单位为Byte) | **示例:** ```js let bundleStats = storageStatistics.getCurrentBundleStats(); console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats)); ``` ## storageStatistics.getCurrentBundleStats9+ getCurrentBundleStats(callback: AsyncCallback<BundleStats>): void 第三方应用异步获取当前应用存储空间大小(单位为Byte),以callback方式返回。 **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | | callback | AsyncCallback<[BundleStats](#bundlestats9)> | 是 | 获取指定卷上的应用存储空间大小之后的回调 | **示例:** ```js storageStatistics.getCurrentBundleStats(function(error, bundleStats){ // do something console.info("getCurrentBundleStats successfully:"+ JSON.stringify(bundleStats)); }); ``` ## BundleStats9+ ### 属性 **系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.StorageService.SpatialStatistics | 名称 | 类型 | 可读 | 可写 | 说明 | | --------- | ------ | --- | ---- | -------------- | | appSize | number | 是 | 否 | app数据大小(单位为Byte) | | cacheSize | number | 是 | 否 | 缓存数据大小(单位为Byte) | | dataSize | number | 是 | 否 | 应用总数据大小(单位为Byte) | ## storageStatistics.getTotalSize9+ getTotalSize(): Promise<number> 获取内置存储的总空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | 返回内置存储的总空间大小(单位为Byte) | **示例:** ```js let number = storageStatistics.getTotalSize(); console.info("getTotalSize successfully:"+ JSON.stringify(number)); ``` ## storageStatistics.getTotalSize9+ getTotalSize(callback: AsyncCallback<number>): void 获取内置存储的总空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------ | ---- | ------------------------ | | callback | AsyncCallback<number> | 是 | 获取内置存储的总空间大小之后的回调 | **示例:** ```js storageStatistics.getTotalSize(function(error, number){ // do something console.info("getTotalSize successfully:"+ JSON.stringify(number)); }); ``` ## storageStatistics.getFreeSize9+ getFreeSize(): Promise<number> 获取内置存储的可用空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | 返回内置存储的可用空间大小(单位为Byte) | **示例:** ```js let number = storageStatistics.getFreeSize(); console.info("getFreeSize successfully:"+ JSON.stringify(number)); ``` ## storageStatistics.getFreeSize9+ getFreeSize(callback: AsyncCallback<number>): void 获取内置存储的可用空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------ | ---- | ------------------------- | | callback | AsyncCallback<number> | 是 | 获取内置存储的可用空间大小之后的回调 | **示例:** ```js storageStatistics.getFreeSize(function(error, number){ // do something console.info("getFreeSize successfully:"+ JSON.stringify(number)); }); ``` ## storageStatistics.getSystemSize9+ getSystemSize(): Promise<number> 异步获取系统数据的空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<number> | 返回系统数据的空间大小(单位为Byte) | **示例:** ```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 异步获取系统数据的空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | callback | AsyncCallback<number> | 是 | 获取系统数据的空间大小之后的回调 | **示例:** ```js storageStatistics.getSystemSize(function(error, number){ // do something console.info("getSystemSize successfully:"+ number); }); ``` ## storageStatistics.getUserStorageStats9+ getUserStorageStats(): Promise<StorageStats> 异步获取当前用户各类别存储空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<[StorageStats](#storagestats9)> | 返回当前用户各类别存储空间大小(单位为Byte) | **示例:** ```js storageStatistics.getUserStorageStats().then(function(StorageStats){ console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); }).catch(function(err){ console.info("getUserStorageStats failed with error:"+ err); }); ``` ## storageStatistics.getUserStorageStats9+ getUserStorageStats(callback: AsyncCallback<StorageStats>): void 异步获取当前用户各类别存储空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | callback | AsyncCallback<[StorageStats](#storagestats9)> | 是 | 返回用户各类别存储空间大小之后的回调 | **示例:** ```js storageStatistics.getUserStorageStats(function(error, StorageStats){ // do something console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); }); ``` getUserStorageStats(userId: number): Promise<StorageStats> 异步获取指定用户各类别存储空间大小(单位为Byte),以promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | userId | number | 是 | 用户id| **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<[StorageStats](#storagestats9)> | 返回指定用户各类别存储空间大小(单位为Byte) | **示例:** ```js let userId = 100; 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 异步获取指定用户各类别存储空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | userId | number | 是 | 用户id | | callback | AsyncCallback<[StorageStats](#storagestats9)> | 是 | 返回各类别数据大小之后的回调 | **示例:** ```js let userId = 100; storageStatistics.getUserStorageStats(userId, function(error, StorageStats){ // do something console.info("getUserStorageStats successfully:"+ JSON.stringify(StorageStats)); }); ``` ## StorageStats9+ ### 属性 **系统能力**:以下各项对应的系统能力均为SystemCapability.FileManagement.StorageService.SpatialStatistics 该接口为系统接口。 | 名称 | 类型 | 可读 | 可写 | 说明 | | --------- | ------ | ---- | ----- | -------------- | | total | number | 是 | 否 | 内置存储总空间大小(单位为Byte) | | audio | number |是 | 否 | 音频数据大小 (单位为Byte) | | video | number | 是 | 否 | 视频数据大小(单位为Byte) | | image | number | 是 | 否 | 图像数据大小 (单位为Byte) | | file | number | 是 | 否 | 文件数据大小 (单位为Byte) | | app | number | 是 | 否 | 应用数据大小(单位为Byte) |