# @ohos.file.storageStatistics (应用空间统计)(系统接口) 该模块提供空间查询相关的常用功能:包括对内外卡的空间查询,对应用分类数据统计的查询,对应用数据的查询等。 > **说明:** > > - 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.file.storageStatistics (应用空间统计)](js-apis-file-storage-statistics.md)。 ## 导入模块 ```ts 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> | Promise对象,返回指定卷设备的总空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600008 | No such object. | | 13900042 | Unknown error. | **示例:** ```ts import volumemanager from "@ohos.file.volumeManager"; import { BusinessError } from '@ohos.base'; volumemanager.getAllVolumes().then((volumes: Array) => { let uuid: string = volumes[0].uuid; storageStatistics.getTotalSizeOfVolume(uuid).then((number: number) => { console.info("getTotalSizeOfVolume successfully:" + number); }).catch((err: BusinessError) => { console.error("getTotalSizeOfVolume failed with error:" + JSON.stringify(err)); }); }).catch((err: BusinessError) => { console.error("getAllVolumes failed with error:" + JSON.stringify(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> | 是 | 获取指定卷设备总空间之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600008 | No such object. | | 13900042 | Unknown error. | **示例:** ```ts import volumemanager from "@ohos.file.volumeManager"; import { BusinessError } from '@ohos.base'; volumemanager.getAllVolumes().then((volumes: Array) => { let uuid: string = volumes[0].uuid; storageStatistics.getTotalSizeOfVolume(uuid, (error: BusinessError, number: number) => { if (error) { console.error("getTotalSizeOfVolume failed with error:" + JSON.stringify(error)); } else { // do something console.info("getTotalSizeOfVolume successfully:" + number); } }); }).catch((err: BusinessError) => { console.error("getAllVolumes failed with error:" + JSON.stringify(err)); }); ``` ## storageStatistics.getFreeSizeOfVolume getFreeSizeOfVolume(volumeUuid: string): Promise<number> 异步获取外置存储设备中指定卷设备的可用空间大小(单位为Byte),以Promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | volumeUuid | string | 是 | 卷设备uuid | **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | Promise对象,返回指定卷的可用空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600008 | No such object. | | 13900042 | Unknown error. | **示例:** ```ts import volumemanager from "@ohos.file.volumeManager"; import { BusinessError } from '@ohos.base'; volumemanager.getAllVolumes().then((volumes: Array) => { let uuid: string = volumes[0].uuid; storageStatistics.getFreeSizeOfVolume(uuid).then((number: number) => { console.info("getFreeSizeOfVolume successfully:" + number); }).catch((err: BusinessError) => { console.error("getFreeSizeOfVolume failed with error:" + JSON.stringify(err)); }); }).catch((err: BusinessError) => { console.error("getAllVolumes failed with error:" + JSON.stringify(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> | 是 | 获取指定卷可用空间之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600008 | No such object. | | 13900042 | Unknown error. | **示例:** ```ts import volumemanager from "@ohos.file.volumeManager"; import { BusinessError } from '@ohos.base'; volumemanager.getAllVolumes().then((volumes: Array) => { let uuid: string = volumes[0].uuid; storageStatistics.getFreeSizeOfVolume(uuid, (error: BusinessError, number: number) => { if (error) { console.error("getFreeSizeOfVolume failed with error:" + JSON.stringify(error)); } else { // do something console.info("getFreeSizeOfVolume successfully: " + number); } }); }).catch((err: BusinessError) => { console.error("getAllVolumes failed with error:" + JSON.stringify(err)); }); ``` ## storageStatistics.getBundleStats9+ getBundleStats(packageName: string, index?: number): Promise<BundleStats> 异步获取应用存储数据的空间大小(单位为Byte),以Promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | -------- | | packageName | string | 是 | 应用包名 | | index12+ | 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)接口获得。| **返回值:** | 类型 | 说明 | | ------------------------------------------ | -------------------------- | | Promise<[Bundlestats](js-apis-file-storage-statistics.md#bundlestats9)> | Promise对象,返回指定卷上的应用存储数据的空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600008 | No such object. | | 13900042 | Unknown error. | **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import storageStatistics from "@ohos.file.storageStatistics"; import { BusinessError } from '@ohos.base'; import { hilog } from '@kit.PerformanceAnalysisKit'; let bundleName = "com.example.myapplication"; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags); hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label)); let packageName:string = bundleName; let index:number = resourceInfo.appIndex; storageStatistics.getBundleStats(packageName, index).then((BundleStats: storageStatistics.BundleStats) => { hilog.info(0x0000, 'testTag', 'getBundleStats successfully. BundleStats: %{public}s', JSON.stringify(BundleStats)); }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', 'getBundleStats failed with error: %{public}s', JSON.stringify(err)); }); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed with error: %{public}s', message); } ``` ## storageStatistics.getBundleStats9+ getBundleStats(packageName: string, callback: AsyncCallback<BundleStats>, index?: number): void 异步获取应用存储数据的空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------------------------------- | ---- | ------------------------------------ | | packageName | string | 是 | 应用包名 | | callback | AsyncCallback<[Bundlestats](js-apis-file-storage-statistics.md#bundlestats9)> | 是 | 获取指定卷上的应用存储数据的空间大小之后的回调 | | index12+ | 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)接口获得。| **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600008 | No such object. | | 13900042 | Unknown error. | **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import storageStatistics from "@ohos.file.storageStatistics"; import { BusinessError } from '@ohos.base'; import { hilog } from '@kit.PerformanceAnalysisKit'; let bundleName = "com.example.myapplication"; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags); hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label)); let packageName:string = bundleName; let index:number = resourceInfo.appIndex; storageStatistics.getBundleStats(packageName, (err: BusinessError, BundleStats: storageStatistics.BundleStats) => { if (err) { hilog.error(0x0000, 'testTag', 'getBundleStats failed with error: %{public}s', JSON.stringify(err)); } else { hilog.info(0x0000, 'testTag', 'getBundleStats successfully. BundleStats: %{public}s', JSON.stringify(BundleStats)); } }, index); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed: %{public}s', message); } ``` ## storageStatistics.getTotalSize9+ getTotalSize(): Promise<number> 获取内置存储的总空间大小(单位为Byte),以Promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | Promise对象,返回内置存储的总空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getTotalSize().then((number: number) => { console.info("getTotalSize successfully:" + JSON.stringify(number)); }).catch((err: BusinessError) => { console.error("getTotalSize failed with error:"+ JSON.stringify(err)); }); ``` ## storageStatistics.getTotalSize9+ getTotalSize(callback: AsyncCallback<number>): void 获取内置存储的总空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------ | ---- | ------------------------ | | callback | AsyncCallback<number> | 是 | 获取内置存储的总空间大小之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getTotalSize((error: BusinessError, number: number) => { if (error) { console.error("getTotalSize failed with error:" + JSON.stringify(error)); } else { // do something console.info("getTotalSize successfully:" + number); } }); ``` ## storageStatistics.getTotalSizeSync10+ getTotalSizeSync(): number 同步获取内置存储的总空间大小(单位为Byte)。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | number | 返回内置存储的总空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { let number = storageStatistics.getTotalSizeSync(); console.info("getTotalSizeSync successfully:" + JSON.stringify(number)); } catch (err) { let error: BusinessError = err as BusinessError; console.error("getTotalSizeSync failed with error:" + JSON.stringify(error)); } ``` ## storageStatistics.getFreeSize9+ getFreeSize(): Promise<number> 获取内置存储的可用空间大小(单位为Byte),以Promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | Promise<number> | Promise对象,返回内置存储的可用空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getFreeSize().then((number: number) => { console.info("getFreeSize successfully:" + JSON.stringify(number)); }).catch((err: BusinessError) => { console.error("getFreeSize failed with error:" + JSON.stringify(err)); }); ``` ## storageStatistics.getFreeSize9+ getFreeSize(callback: AsyncCallback<number>): void 获取内置存储的可用空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------ | ---- | ------------------------- | | callback | AsyncCallback<number> | 是 | 获取内置存储的可用空间大小之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getFreeSize((error: BusinessError, number: number) => { if (error) { console.error("getFreeSize failed with error:" + JSON.stringify(error)); } else { // do something console.info("getFreeSize successfully:" + number); } }); ``` ## storageStatistics.getFreeSizeSync10+ getFreeSizeSync(): number 同步获取内置存储的可用空间大小(单位为Byte)。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ------------------ | | number | 返回内置存储的可用空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; try { let number = storageStatistics.getFreeSizeSync(); console.info("getFreeSizeSync successfully:" + JSON.stringify(number)); } catch (err) { let error: BusinessError = err as BusinessError; console.error("getFreeSizeSync failed with error:" + JSON.stringify(error)); } ``` ## storageStatistics.getSystemSize9+ getSystemSize(): Promise<number> 异步获取系统数据的空间大小(单位为Byte),以Promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<number> | Promise对象,返回系统数据的空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getSystemSize().then((number: number) => { console.info("getSystemSize successfully:" + number); }).catch((err: BusinessError) => { console.error("getSystemSize failed with error:" + JSON.stringify(err)); }); ``` ## storageStatistics.getSystemSize9+ getSystemSize(callback: AsyncCallback<number>): void 异步获取系统数据的空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | callback | AsyncCallback<number> | 是 | 获取系统数据的空间大小之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getSystemSize((error: BusinessError, number: number) => { if (error) { console.error("getSystemSize failed with error:" + JSON.stringify(error)); } else { // 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)> | Promise对象,返回当前用户各类别存储空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getUserStorageStats().then((storageStats: storageStatistics.StorageStats) => { console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats)); }).catch((err: BusinessError) => { console.error("getUserStorageStats failed with error:" + JSON.stringify(err)); }); ``` ## storageStatistics.getUserStorageStats9+ getUserStorageStats(callback: AsyncCallback<StorageStats>): void 异步获取当前用户各类别存储空间大小(单位为Byte),以callback方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------------------------------------ | ---- | -------------------------- | | callback | AsyncCallback<[StorageStats](#storagestats9)> | 是 | 返回用户各类别存储空间大小之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; storageStatistics.getUserStorageStats((error: BusinessError, storageStats: storageStatistics.StorageStats) => { if (error) { console.error("getUserStorageStats failed with error:" + JSON.stringify(error)); } else { // do something console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats)); } }); ``` ## storageStatistics.getUserStorageStats9+ getUserStorageStats(userId: number): Promise<StorageStats> 异步获取指定用户各类别存储空间大小(单位为Byte),以Promise方式返回。 **需要权限**:ohos.permission.STORAGE_MANAGER **系统能力**:SystemCapability.FileManagement.StorageService.SpatialStatistics **系统接口:** 该接口为系统接口。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ------ | ---- | ---- | | userId | number | 是 | 用户id| **返回值:** | 类型 | 说明 | | --------------------- | ---------------- | | Promise<[StorageStats](#storagestats9)> | Promise对象,返回指定用户各类别存储空间大小(单位为Byte) | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600009 | User if out of range. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; let userId: number = 100; storageStatistics.getUserStorageStats(userId).then((storageStats: storageStatistics.StorageStats) => { console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats)); }).catch((err: BusinessError) => { console.error("getUserStorageStats failed with error:" + JSON.stringify(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)> | 是 | 返回指定用户各类别存储空间大小之后的回调 | **错误码:** 以下错误码的详细介绍请参见[文件管理错误码](errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | -------- | | 201 | Permission verification failed. | | 202 | The caller is not a system application. | | 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | | 13600001 | IPC error. | | 13600009 | User if out of range. | | 13900042 | Unknown error. | **示例:** ```ts import { BusinessError } from '@ohos.base'; let userId: number = 100; storageStatistics.getUserStorageStats(userId, (error: BusinessError, storageStats: storageStatistics.StorageStats) => { if (error) { console.error("getUserStorageStats failed with error:" + JSON.stringify(error)); } else { // 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) |