@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 "@ohos.file.storageStatistics";

storageStatistics.getCurrentBundleStats9+

getCurrentBundleStats(): Promise<BundleStats>

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

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Return value

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

Error codes

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

ID Error Message
401 The input parameter is invalid.
13600001 IPC error.
13900042 Unknown error.

Example

import { BusinessError } from '@ohos.base';
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 of this application, in bytes. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Parameters

Name Type Mandatory Description
callback AsyncCallback<BundleStats> Yes Callback invoked to return the application space obtained.

Error codes

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

ID Error Message
401 The input parameter is invalid.
13600001 IPC error.
13900042 Unknown error.

Example

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

BundleStats9+

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Name Type Readable Writable Description
appSize number Yes No Size of the application data, in bytes.
cacheSize number Yes No Size of the cache data, in bytes.
dataSize number Yes No Total size of the application, in bytes.