Zum Inhalt springen

Application and file system space statistics

API Introduction

For detailed API information, refer to ohos.file.statvfs and ohos.file.storageStatistics.

File System and Application Space Statistics

Module Interface Name Function
@ohos.file.storageStatistics getCurrentBundleStats Gets the storage size of the current app (in bytes).
@ohos.file.statvfs getFreeSize Gets the remaining space of the specified file system (in bytes).
@ohos.file.statvfs getTotalSize Gets the total space of the specified file system (in bytes).

Application Space Statistics

BundleStats Property Meaning Statistical Path
appSize Size of the app installation file (in bytes). App installation files are stored in: /data/storage/el1/bundle.
cacheSize Size of the app cache files (in bytes). App cache files are stored in: /data/storage/el1/base/cache, /data/storage/el1/base/haps/entry/cache, /data/storage/el2/base/cache, /data/storage/el2/base/haps/entry/cache.
dataSize Size of app file storage (excluding installation and cache files, in bytes). App files include local, distributed, and database files. Local files are stored in: /data/storage/el1/base, /data/storage/el2/base. Distributed files are stored in: /data/storage/el2/distributedfiles. Database files are stored in: /data/storage/el1/database, /data/storage/el2/database.

Development Examples

Getting the Remaining Space of the File System’s Data Partition

import { statfs } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';

let context = getContext(this) as common.UIAbilityContext;
let path = context.filesDir;
statfs.getFreeSize(path, (err: BusinessError, number: number) => {
  if (err) {
    console.error(`Invoke getFreeSize failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info(`Invoke getFreeSize succeeded, size is ${number}`);
  }
});

Getting the Storage Space Size of the Current Application

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

storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {
  if (err) {
    console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info(`Invoke getCurrentBundleStats succeeded, appsize is ${bundleStats.appSize}`);
  }
});

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert