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

2025-06-12 浏览 (1)

@ohos.file.storageStatistics (Application Storage Statistics)

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.

Modules to Import

import  { storageStatistics } from '@kit.CoreFileKit';

storageStatistics.getCurrentBundleStats9+

getCurrentBundleStats(): Promise<BundleStats>

Obtains the storage space (in bytes) of this application. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Return value

TypeDescription
Promise<Bundlestats>Promise used to return the application storage space obtained.

Error codes

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

IDError Message
401The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
storageStatistics.getCurrentBundleStats().then((BundleStats: storageStatistics.BundleStats) => {
  console.info("getCurrentBundleStats successfully:" + JSON.stringify(BundleStats));
}).catch((err: BusinessError) => {
  console.error("getCurrentBundleStats failed with error:"+ JSON.stringify(err));
});

storageStatistics.getCurrentBundleStats9+

getCurrentBundleStats(callback: AsyncCallback<BundleStats>): void

Obtains the storage space (in bytes) of this application. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<BundleStats>YesCallback used to return the application space obtained.

Error codes

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

IDError Message
401The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
  if (error) {
    console.error("getCurrentBundleStats failed with error:" + JSON.stringify(error));
  } else {
    // Do something.
    console.info("getCurrentBundleStats successfully:" + JSON.stringify(bundleStats));
  }
});

storageStatistics.getTotalSize15+

getTotalSize(): Promise<number>

Obtains the total space of the built-in storage, in bytes. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Return value

TypeDescription
Promise<number>Promise used to return the total built-in storage space obtained.

Error codes

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

IDError Message
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.getTotalSize15+

getTotalSize(callback: AsyncCallback<number>): void

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

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the built-in storage space obtained.

Error codes

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

IDError Message
401The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.getTotalSizeSync15+

getTotalSizeSync(): number

Obtains the total space of the built-in storage, in bytes. This API returns the result synchronously.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Return value

TypeDescription
numberBuilt-in storage space obtained.

Error codes

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

IDError Message
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.getFreeSize15+

getFreeSize(): Promise<number>

Obtains the available space of the built-in storage, in bytes. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Return value

TypeDescription
Promise<number>Promise used to return the available space of the built-in storage obtained.

Error codes

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

IDError Message
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.getFreeSize15+

getFreeSize(callback: AsyncCallback<number>): void

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

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the available space of the built-in storage obtained.

Error codes

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

IDError Message
401The input parameter is invalid. Possible causes: Mandatory parameters are left unspecified.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.getFreeSizeSync15+

getFreeSizeSync(): number

Obtains the available space of the built-in storage, in bytes. This API returns the result synchronously.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Return value

TypeDescription
numberAvailable space of the built-in storage obtained.

Error codes

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

IDError Message
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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));
}

BundleStats9+

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

NameTypeMandatoryDescription
appSizenumberYesSize of the application installation files, in bytes.
cacheSizenumberYesSize of the application cache files, in bytes.
dataSizenumberYesSize of other files of the application, 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/ca03HC