@ohos.file.storageStatistics (应用空间统计)(系统接口)

该模块提供空间查询相关的常用功能:包括对内外卡的空间查询、对应用分类数据统计的查询、对应用数据的查询等。

说明:

  • 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
  • 当前页面仅包含本模块的系统接口,其他公开接口参见@ohos.file.storageStatistics (应用空间统计)

导入模块

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)。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';

volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  if (volumes == null || volumes.length <= 0) {
    console.error("volumes is null or length is invalid");
    return;
  }
  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> 获取指定卷设备总空间之后的回调。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';

volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  if (volumes == null || volumes.length <= 0) {
    console.error("volumes is null or length is invalid");
    return;
  }
  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)。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';

volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  if (volumes == null || volumes.length <= 0) {
    console.error("volumes is null or length is invalid");
    return;
  }
  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> 获取指定卷可用空间之后的回调。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';

volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  if (volumes == null || volumes.length <= 0) {
    console.error("volumes is null or length is invalid");
    return;
  }
  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的appIndex属性,后续可以通过调用getBundleResourceInfo接口获得。

返回值:

类型 说明
Promise<Bundlestats> Promise对象,返回指定卷上的应用存储数据的空间大小(单位为Byte)。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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> 获取指定卷上的应用存储数据的空间大小之后的回调。
index12+ number 分身应用的索引号,默认值为0(表示未分身的主应用)。分身应用索引号在分身创建时默认占用从1开始且当前未被占用的最小索引号,并赋值给该应用的BundleResourceInfo的appIndex属性,后续可以通过调用getBundleResourceInfo接口获得。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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.getSystemSize9+

getSystemSize(): Promise<number>

异步获取系统数据的空间大小(单位为Byte),以Promise方式返回。

需要权限:ohos.permission.STORAGE_MANAGER

系统能力:SystemCapability.FileManagement.StorageService.SpatialStatistics

系统接口:该接口为系统接口。

返回值:

类型 说明
Promise<number> Promise对象,返回系统数据的空间大小(单位为Byte)。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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> 获取系统数据的空间大小之后的回调。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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> Promise对象,返回当前用户各类别存储空间大小(单位为Byte)。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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> 返回用户各类别存储空间大小之后的回调。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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> Promise对象,返回指定用户各类别存储空间大小(单位为Byte)。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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> 返回指定用户各类别存储空间大小之后的回调。

错误码:

以下错误码的详细介绍请参见文件管理错误码

错误码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.

示例:

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。