鸿蒙中 系统存储空间获取
·
本文同步发表于我的微信公众号,微信搜索 程语新视界 即可关注,每个工作日都有文章更新
开发中,系统空间不足、缓存目录配额限制等问题可能导致应用崩溃或功能异常。所以如何获取和监控文件系统空间及应用自身存储使用情况,可以帮助构建更健壮的应用。
二、核心 API 模块
2.1 两大核心模块
| 模块 | 作用 | 引入方式 |
|---|---|---|
| @ohos.file.storageStatistics | 应用存储空间统计 | import { storageStatistics } from '@kit.CoreFileKit'; |
| @ohos.file.statvfs | 文件系统空间统计 | import { statfs } from '@kit.CoreFileKit'; |
三、API 说明
3.1 文件系统空间统计接口
| 模块 | 接口名 | 功能 | 支持版本 |
|---|---|---|---|
| @ohos.file.statvfs | getFreeSize(path: string) |
获取指定文件系统的剩余空间(字节) | API 20+ |
| @ohos.file.statvfs | getTotalSize(path: string) |
获取指定文件系统的总空间(字节) | API 20+ |
3.2 应用空间统计接口
| 模块 | 接口名 | 功能 | 支持版本 |
|---|---|---|---|
| @ohos.file.storageStatistics | getCurrentBundleStats() |
获取当前应用存储空间大小 | 所有版本 |
| @ohos.file.storageStatistics | getFreeSize() |
异步获取内置存储可用空间 | API 15+ |
| @ohos.file.storageStatistics | getFreeSizeSync() |
同步获取内置存储可用空间 | API 15+ |
| @ohos.file.storageStatistics | getTotalSize() |
异步获取内置存储总空间 | API 15+ |
| @ohos.file.storageStatistics | getTotalSizeSync() |
同步获取内置存储总空间 | API 15+ |
四、BundleStats 属性
4.1 属性说明
| 属性 | 含义 | 统计路径 |
|---|---|---|
| appSize | 应用安装文件大小 | /data/storage/el1/bundle |
| cacheSize | 应用缓存文件大小 | /data/storage/${el1-el5}/base/cache/data/storage/${el1-el5}/base/haps/${moduleName}/cache |
| dataSize | 应用数据文件大小 | 本地文件:/data/storage/${el1-el5}/base分布式文件: /data/storage/el2/distributedfiles数据库文件: /data/storage/${el1-el5}/database |
4.2 沙箱目录
进入应用沙箱命令:
# 1. 连接设备
hdc shell
# 2. 进入指定进程的沙箱空间
nsenter -t {pid} -m sh
# 示例:进入进程ID为1234的应用沙箱
nsenter -t 1234 -m sh
EL级别说明:
-
el1-el5表示不同安全等级的沙箱目录 -
不同EL级别对应不同的数据隔离级别
五、代码示例
5.1 获取文件系统剩余空间
import { statfs } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
// 获取应用上下文
let context = this.getUIContext().getHostContext() 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}`);
}
});
5.2 获取当前应用存储空间
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}`);
console.info(`Cache size: ${bundleStats.cacheSize}`);
console.info(`Data size: ${bundleStats.dataSize}`);
}
});
5.3 异步获取内置存储空间
import { storageStatistics } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
// 异步获取总空间
storageStatistics.getTotalSize().then((number: number) => {
console.info(`getTotalSize successfully, number is ${number}`);
}).catch((err: BusinessError) => {
console.error(`getTotalSize failed with error, code is ${err.code}, message is ${err.message}`);
});
// 异步获取可用空间
storageStatistics.getFreeSize().then((number: number) => {
console.info(`getFreeSize successfully, number is ${number}`);
}).catch((err: BusinessError) => {
console.error(`getFreeSize failed with error, code is ${err.code}, message is ${err.message}`);
});
5.4 同步获取内置存储空间
import { storageStatistics } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
// 同步获取总空间
try {
let number = storageStatistics.getTotalSizeSync();
console.info(`getTotalSizeSync successfully, number is ${number}`);
} catch (err) {
let error: BusinessError = err as BusinessError;
console.error(`getTotalSizeSync failed with error, code is ${error.code}, message is ${error.message}`);
}
// 同步获取可用空间
try {
let number = storageStatistics.getFreeSizeSync();
console.info(`getFreeSizeSync successfully, number is ${number}`);
} catch (err) {
let error: BusinessError = err as BusinessError;
console.error(`getFreeSizeSync failed with error, code is ${error.code}, message is ${error.message}`);
}
更多推荐
所有评论(0)