HarmonyOS鸿蒙Next中如何获取系统的可用空间和总容量?

HarmonyOS鸿蒙Next中如何获取系统的可用空间和总容量? 如何获取系统的可用空间和总容量

HarmonyOS3.1

我没查到相关API

2 回复

当前只有系统应用可以使用该能力,三方应用暂时只能统计自己的空间。

https://docs.openharmony.cn/pages/v3.2/zh-cn/application-dev/reference/apis/js-apis-file-storage-statistics.md/#storagestatisticsgettotalsize9

更多关于HarmonyOS鸿蒙Next中如何获取系统的可用空间和总容量?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,可以通过StorageManager类获取系统的可用空间和总容量。首先,使用getSystemService(Context.STORAGE_SERVICE)获取StorageManager实例,然后调用getStorageVolumes()获取存储卷列表。通过StorageVolumegetPath()方法获取路径,再使用StatFs类计算可用空间和总容量。具体代码示例如下:

StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
for (StorageVolume volume : storageVolumes) {
    String path = volume.getPath();
    StatFs statFs = new StatFs(path);
    long availableSpace = statFs.getAvailableBytes();
    long totalSpace = statFs.getTotalBytes();
    Log.d("StorageInfo", "Path: " + path + ", Available: " + availableSpace + ", Total: " + totalSpace);
}

此代码将遍历所有存储卷,并输出每个卷的路径、可用空间和总容量。

回到顶部