openharmony 鸿蒙 js-apis-file-storage-statistics-sys

2025-06-12 浏览 (1)

@ohos.file.storageStatistics (Application Storage Statistics) (System API)

The storageStatistics module provides APIs for obtaining storage space information, including the space of built-in and plug-in memory cards, space occupied by different types of data, and space of application data.

NOTE

  • The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.file.storageStatistics.

Modules to Import

import storageStatistics from "@ohos.file.storageStatistics";

storageStatistics.getTotalSizeOfVolume

getTotalSizeOfVolume(volumeUuid: string): Promise<number>

Obtains the total space of a volume in an external storage device, in bytes. This API uses a promise to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
volumeUuidstringYesUUID of the volume.

Return value

TypeDescription
Promise<number>Promise used to return the total volume space (in bytes) obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';
volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  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

Obtains the total space of a volume in an external storage device, in bytes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
volumeUuidstringYesUUID of the volume.
callbackAsyncCallback<number>YesCallback used to return the total volume space obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';
volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  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>

Obtains the available space of a volume in an external storage device, in bytes. This API uses a promise to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
volumeUuidstringYesUUID of the volume.

Return value

TypeDescription
Promise<number>Promise used to return the available volume space (in bytes) obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';
volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  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

Obtains the available space of a volume in an external storage device, in bytes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
volumeUuidstringYesUUID of the volume.
callbackAsyncCallback<number>YesCallback used to return the available volume space obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import volumemanager from "@ohos.file.volumeManager";
import { BusinessError } from '@ohos.base';
volumemanager.getAllVolumes().then((volumes: Array<volumemanager.Volume>) => {
  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>

Obtains the storage space of an application, in bytes. This API uses a promise to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
packageNamestringYesPackage name of the application.
index12+numberNoIndex of an application clone. The default value is 0, which indicates the application itself. When an application clone is created, an index is assigned from 1 sequentially to appIndex of BundleResourceInfo. The index can be obtained by getBundleResourceInfo.

Return value

TypeDescription
Promise<Bundlestats>Promise used to return the application storage space (in bytes) obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

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

Obtains the storage space of an application, in bytes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
packageNamestringYesPackage name of the application.
callbackAsyncCallback<Bundlestats>YesCallback used to return the application storage space obtained.
index12+numberNoIndex of an application clone. The default value is 0, which indicates the application itself. When an application clone is created, an index is assigned from 1 sequentially to appIndex of BundleResourceInfo. The index can be obtained by getBundleResourceInfo.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

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>

Obtains the system data size, in bytes. This API uses a promise to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Return value

TypeDescription
Promise<number>Promise used to return the system data size (in bytes) obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified.
13600001IPC error.
13900042Unknown error.

Example

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

Obtains the system data size, in bytes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the system data size obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified.
13600001IPC error.
13900042Unknown error.

Example

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>

Obtains the storage statistics of this user, in bytes. This API uses a promise to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Return value

TypeDescription
Promise<StorageStats>Promise used to return the storage statistics (in bytes) obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13900042Unknown error.

Example

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

Obtains the storage statistics of this user, in bytes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<StorageStats>YesCallback used to return the storage statistics obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13900042Unknown error.

Example

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>

Obtains the storage statistics of the specified user, in bytes. This API uses a promise to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.

Return value

TypeDescription
Promise<StorageStats>Promise used to return the storage statistics (in bytes) obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600009User if out of range.
13900042Unknown error.

Example

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

Obtains the storage statistics of the specified user, in bytes. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.
callbackAsyncCallback<StorageStats>YesCallback used to return the storage statistics obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13600009User if out of range.
13900042Unknown error.

Example

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+

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

NameTypeRead-OnlyWritableDescription
totalnumberYesNoTotal space of the built-in storage, in bytes.
audionumberYesNoSize of the audio data, in bytes.
videonumberYesNoSize of the video data, in bytes.
imagenumberYesNoSize of the image data, in bytes.
filenumberYesNoSize of files, in bytes.
appnumberYesNoSize of application data, in bytes.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/Hn7wD7