harmony 鸿蒙@ohos.file.storageStatistics (Application Storage Statistics)

2023-06-24 浏览 (942)

@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.getTotalSizeOfVolume

getTotalSizeOfVolume(volumeUuid: string): Promise<number>

Obtains the total size (in bytes) of the specified volume in an external storage device. 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 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.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
let uuid: string = "";
storageStatistics.getTotalSizeOfVolume(uuid).then((number: number) => {
  console.info("getTotalSizeOfVolume successfully:" + number);
}).catch((err: BusinessError) => {
  console.info("getTotalSizeOfVolume failed with error:" + JSON.stringify(err));
});

storageStatistics.getTotalSizeOfVolume

getTotalSizeOfVolume(volumeUuid: string, callback: AsyncCallback<number>): void

Obtains the total size (in bytes) of the specified volume in an external storage device. 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 invoked to return the total volume 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.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
let uuid: string = "";
storageStatistics.getTotalSizeOfVolume(uuid, (error: BusinessError, number: number) => {
  // Do something.
  console.info("getTotalSizeOfVolume successfully:" + number);
});

storageStatistics.getFreeSizeOfVolume

getFreeSizeOfVolume(volumeUuid: string): Promise<number>

Obtains the available space (in bytes) of the specified volume in an external storage device. 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 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.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
let uuid: string = "";
storageStatistics.getFreeSizeOfVolume(uuid).then((number: number) => {
  console.info("getFreeSizeOfVolume successfully:" + number);
}).catch((err: BusinessError) => {
  console.info("getFreeSizeOfVolume failed with error:" + JSON.stringify(err));
});

storageStatistics.getFreeSizeOfVolume

getFreeSizeOfVolume(volumeUuid: string, callback: AsyncCallback<number>): void

Obtains the available space (in bytes) of the specified volume in an external storage device. 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 invoked 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.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
let uuid: string = "";
storageStatistics.getFreeSizeOfVolume(uuid, (error: BusinessError, number: number) => {
  // Do something.
  console.info("getFreeSizeOfVolume successfully: " + number);
});

storageStatistics.getBundleStats9+

getBundleStats(packageName: string): Promise<BundleStats>

Obtains the space (in bytes) of an application. 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
packageNamestringYesBundle name of the application.

Return value

TypeDescription
Promise<Bundlestats>Promise used to return the application 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.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
let packageName: string = "";
storageStatistics.getBundleStats(packageName).then((BundleStats: storageStatistics.Bundlestats) => {
  console.info("getBundleStats successfully:" + JSON.stringify(BundleStats));
}).catch((err: BusinessError) => {
  console.info("getBundleStats failed with error:" + JSON.stringify(err));
});

storageStatistics.getBundleStats9+

getBundleStats(packageName: string, callback: AsyncCallback<BundleStats>): void

Obtains the space (in bytes) of an application. 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
packageNamestringYesBundle name of the application.
callbackAsyncCallback<Bundlestats>YesCallback invoked to return the application 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.
13600001IPC error.
13600008No such object.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
let packageName: string = "";
storageStatistics.getBundleStats(packageName, (error: BusinessError, BundleStats: storageStatistics.Bundlestats) => {
  // Do something.
  console.info("getBundleStats successfully:" + JSON.stringify(BundleStats));
});

storageStatistics.getCurrentBundleStats9+

getCurrentBundleStats(): Promise<BundleStats>

Obtains the space (in bytes) of this third-party 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.
13600001IPC error.
13900042Unknown error.

Example

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

storageStatistics.getCurrentBundleStats9+

getCurrentBundleStats(callback: AsyncCallback<BundleStats>): void

Obtains the space (in bytes) of this third-party application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<BundleStats>YesCallback invoked 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.Bundlestats) => {
  // Do something.
  console.info("getCurrentBundleStats successfully:" + JSON.stringify(bundleStats));
});

BundleStats9+

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

NameTypeReadableWritableDescription
appSizenumberYesNoSize of the application, in bytes.
cacheSizenumberYesNoCache size of the application, in bytes.
dataSizenumberYesNoTotal data size of the application, in bytes.

storageStatistics.getTotalSize9+

getTotalSize(): Promise<number>

Obtains the total size (in bytes) of the built-in storage. 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 total built-in storage 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getTotalSize().then((number: number) => {
  console.info("getTotalSize successfully:" + JSON.stringify(number));
}).catch((err: BusinessError) => {
  console.info("getTotalSize failed with error:"+ JSON.stringify(err));
});

storageStatistics.getTotalSize9+

getTotalSize(callback: AsyncCallback<number>): void

Obtains the total size (in bytes) of the built-in storage. 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 invoked to return the built-in storage 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getTotalSize((error: BusinessError, number: number) => {
  // Do something.
  console.info("getTotalSize successfully:"+ JSON.stringify(number));
});

storageStatistics.getTotalSizeSync10+

getTotalSizeSync(): number

Obtains the total space (in bytes) of the built-in storage synchronously.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
try {
  let number = storageStatistics.getTotalSizeSync();
  console.info("getTotalSizeSync successfully:" + JSON.stringify(number));
} catch (error) {
  let error: BusinessError = err as BusinessError;
  console.info("getTotalSizeSync failed with error:" + JSON.stringify(err));
}

storageStatistics.getFreeSize9+

getFreeSize(): Promise<number>

Obtains the available space (in bytes) of the built-in storage. 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 available space of the built-in storage 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getFreeSize().then((number: number) => {
  console.info("getFreeSize successfully:" + JSON.stringify(number));
}).catch((err: BusinessError) => {
  console.info("getFreeSize failed with error:" + JSON.stringify(err));
});

storageStatistics.getFreeSize9+

getFreeSize(callback: AsyncCallback<number>): void

Obtains the available space (in bytes) of the built-in storage. 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 invoked 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
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getFreeSize((error: BusinessError, number: number) => {
  // Do something.
  console.info("getFreeSize successfully:" + JSON.stringify(number));
});

storageStatistics.getFreeSizeSync10+

getFreeSizeSync(): number

Obtains the available space (in bytes) of the built-in storage synchronously.

Required permissions: ohos.permission.STORAGE_MANAGER

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
try {
  let number = storageStatistics.getFreeSizeSync();
  console.info("getFreeSizeSync successfully:" + JSON.stringify(number));
} catch (error) {
  let error: BusinessError = err as BusinessError;
  console.info("getFreeSizeSync failed with error:" + JSON.stringify(err));
}

storageStatistics.getSystemSize9+

getSystemSize(): Promise<number>

Obtains the system data space, 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 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getSystemSize().then((number: number) => {
  console.info("getSystemSize successfully:" + number);
}).catch((err: BusinessError) => {
  console.info("getSystemSize failed with error:" + JSON.stringify(err));
});

storageStatistics.getSystemSize9+

getSystemSize(callback: AsyncCallback<number>): void

Obtains the system data space, 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 invoked to return the system data 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getSystemSize((error: BusinessError, number: number) => {
  // Do something.
  console.info("getSystemSize successfully:" + number);
});

storageStatistics.getUserStorageStats9+

getUserStorageStats(): Promise<StorageStats>

Obtains the storage statistics (in bytes) of this user. 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 information 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.
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.info("getUserStorageStats failed with error:" + JSON.stringify(err));
});

storageStatistics.getUserStorageStats9+

getUserStorageStats(callback: AsyncCallback<StorageStats>): void

Obtains the storage statistics (in bytes) of this user. 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 invoked to return the information 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.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@ohos.base';
storageStatistics.getUserStorageStats((error: BusinessError, storageStats: storageStatistics.StorageStats) => {
  // Do something.
  console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats));
});

storageStatistics.getUserStorageStats9+

getUserStorageStats(userId: number): Promise<StorageStats>

Obtains the storage statistics (in bytes) of the specified user. 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 information 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.
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.info("getUserStorageStats failed with error:" + JSON.stringify(err));
});

storageStatistics.getUserStorageStats9+

getUserStorageStats(userId: number, callback: AsyncCallback<StorageStats>): void

Obtains the storage statistics (in bytes) of the specified user. 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 invoked to return the information 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.
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) => {
  // Do something.
  console.info("getUserStorageStats successfully:" + JSON.stringify(storageStats));
});

StorageStats9+

System capability: SystemCapability.FileManagement.StorageService.SpatialStatistics

System API: This is a system API.

NameTypeReadableWritableDescription
totalnumberYesNoTotal size of the built-in storage, in bytes.
audionumberYesNoSpace occupied by audio data, in bytes.
videonumberYesNoSpace occupied by video data, in bytes.
imagenumberYesNoSpace occupied by image data, in bytes.
filenumberYesNoSpace occupied by files, in bytes.
appnumberYesNoSpace occupied by application data, in bytes.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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