安卓获取内部存储信息

目录


前言

原生系统设置里的存储容量到底是怎么计算的,跟踪源码,涉及到VolumeInfo、StorageManagerVolumeProvider、PrivateStorageInfo、StorageStatsManager......等等,java上层没有办法使用简单的api获取到吗?搞了半天,总算获取到了,话不多说,直接上代码。

获取存储容量

java 复制代码
    /**
     * 内部总存储空间
     */
    public static long getStorageTotal(Context context) {
        StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(STORAGE_STATS_SERVICE);
        try {
            return storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 内部可用空间
     */
    public static long getStorageFree(Context context) {
        StorageStatsManager storageStatsManager = (StorageStatsManager) context.getSystemService(STORAGE_STATS_SERVICE);
        try {
            return storageStatsManager.getFreeBytes(StorageManager.UUID_DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * SD卡总空间
     */
    public static long getSdCardTotal(Context context) {
        // 获取所有可用的存储卷
        File[] externalFiles = context.getExternalFilesDirs(null);
        for (File file : externalFiles) {
            if (Environment.isExternalStorageRemovable(file) && !file.getAbsolutePath().contains("emulated")) {
                StatFs stat = new StatFs(file.getPath());
                long blockSize = stat.getBlockSizeLong();
                long totalBlocks = stat.getBlockCountLong();
                return totalBlocks * blockSize;
            }
        }
        return 0;
    }


    /**
     * SD卡可用空间
     */
    public static long getSdCardFree(Context context) {
        // 获取所有可用的存储卷
        File[] externalFiles = context.getExternalFilesDirs(null);
        for (File file : externalFiles) {
            if (Environment.isExternalStorageRemovable(file) && !file.getAbsolutePath().contains("emulated")) {
                StatFs stat = new StatFs(file.getPath());
                long blockSize = stat.getBlockSizeLong();
                long availableBlocks = stat.getAvailableBlocksLong();
                return availableBlocks * blockSize;
            }
        }
        return 0;
    }


    // 格式化文件大小为人类可读的格式
    private static String formatFileSize(long size) {
        String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double fileSize = size;
        while (fileSize > 1000 && unitIndex < units.length - 1) {
            fileSize = fileSize / 1000;
            unitIndex++;
        }
        return String.format("%.2f %s", fileSize, units[unitIndex]);
    }

打印下log看一下:


细心的小伙伴肯定发现单位换算是1000而不是1024了,为什么呢?

其实跟我们电脑上的硬盘存储一样,我们买硬盘的时候会发现1T的容量实际只有930G左右,厂商会解释说那是因为电脑系统采用的1024进制造成容量识别误差,因为厂商们生产的时候都是按照1T=1000G...1KB=1000B的进制的,现在手机厂商也学了这一套,标出64G,128G的容量也是按照1000进制的。 奸商啊,要是1024岂不是能放更多东西,哼,

UUID是什么?

UUID_DEFAULT 是一个常量,表示默认存储的 UUID。在这里,它代表默认存储卷的 UUID,通常指的是内部存储或者主要的外部存储(比如 SD 卡)。

在 Android 设备上,不同的存储卷可以具有不同的标识符(UUID),用于唯一标识每个存储卷。StorageManager.UUID_DEFAULT 是指向默认存储卷的 UUID,因此在获取存储信息时,使用这个 UUID 可以获得默认存储卷的存储空间信息。

更进一步的了解,参考谷歌官方:
UUID

StatFs

另外一点就是StatFs,StatFs 类是 Android 提供的用于获取文件系统信息的类,通常使用 StatFs 类来获取存储卷(如内部存储、外部 SD 卡)的文件系统信息,在应用程序中对存储空间进行监测和管理。通过 StatFs 可以获取存储卷的总大小、可用大小、文件块的大小等信息,我们这里就是使用不同文件系统的块数*每块的字节数进而获取总大小的。

相关推荐
gxgldyh1 小时前
Android Framework源码解析(七):BootAnimation 启动流程解析——开机动画是如何显示出来的?
android
达达尼昂1 小时前
在 Claude Cowork 中用好 Claude Fable 5
android·人工智能·后端
sTone873752 小时前
写时复制COW的第一性理解
android·c++·flutter
sTone873752 小时前
Zygote的第一性理解
android
sTone873752 小时前
类RN框架通过Service暴露卡片渲染能力给AI Chat
android·react native
灯火不休2 小时前
Android Studio & Flutter 构建命令完全指南
android
杉氧2 小时前
Ktor 全栈之路 (5):JWT 认证全流程实战 —— 打造安全通信闭环
android·架构·kotlin
️学习的小王2 小时前
Python + MySQL 学生信息管理系统实战教程
android·python·mysql
帅次3 小时前
Android 高级工程师面试:Flutter 路由导航 近1年高频追问 18 题
android·flutter·面试
alexhilton11 小时前
MVI模式的完整历史、误解和现代Android范式
android·kotlin·android jetpack