Android13获取存储空间大小

内部存储

1、总大小
java 复制代码
public static long getInternalStorageSize(Context context) {
    File filesDir = context.getFilesDir();
    return filesDir.getTotalSpace();
}
2、可用空间大小
java 复制代码
public static long getFreeSpace(Context context) {
    File filesDir = context.getFilesDir();
    return filesDir.getFreeSpace();
}

扩展TF卡

1、总大小
java 复制代码
public static long getExternalStorageSize(Context context) {
    StorageManager storageManager = context.getSystemService(StorageManager.class);
    for (StorageVolume volume : storageManager.getStorageVolumes()) {
        if (volume.isRemovable()) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
                File directory = volume.getDirectory();
                LogUtils.i("fileUtils", "directory = " + directory.getAbsolutePath());
                LogUtils.i("fileUtils", "directory = " + directory.getTotalSpace());
                return directory.getTotalSpace();
            }
        } else {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
                File directory = volume.getDirectory();
                LogUtils.i("fileUtils", "false directory = " + directory.getAbsolutePath());
                LogUtils.i("fileUtils", "false directory = " + directory.getTotalSpace());
            }
        }
    }
    return 0;
}
2、可用空间大小
java 复制代码
public static long getExternalStorageFreeSize(Context context) {
    StorageManager storageManager = context.getSystemService(StorageManager.class);
    for (StorageVolume volume : storageManager.getStorageVolumes()) {
        if (volume.isRemovable()) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
                File directory = volume.getDirectory();
                LogUtils.i("fileUtils", "directory = " + directory.getAbsolutePath());
                LogUtils.i("fileUtils", "free = " + directory.getFreeSpace());
                return directory.getFreeSpace();
            }
        } else {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
                File directory = volume.getDirectory();
                LogUtils.i("fileUtils", "false directory = " + directory.getAbsolutePath());
                LogUtils.i("fileUtils", "false directory = " + directory.getTotalSpace());
            }
        }
    }
    return 0;
}

单位转换

java 复制代码
public static String storageSizeConversion(long size) {
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    if (size < 1024) {
        fileSizeString = df.format((double) size) + "B";
    } else if (size < 1048576) {
        fileSizeString = df.format((double) size / 1024) + "K";
    } else if (size < 1073741824) {
        fileSizeString = df.format((double) size / 1048576) + "M";
    } else {
        fileSizeString = df.format((double) size / 1073741824) + "G";
    }
    return fileSizeString;
}
相关推荐
素师良码5 小时前
第5篇:显示驱动必备调试工具浅析
android
维克兜率天5 小时前
【维克】配对交易的季节性:哪些品种适合长拿?
android·开发语言·笔记·python·算法·kotlin·量化
墨天梦6 小时前
B06_XML控件布局与ViewBinding
android·kotlin
晚风叙码6 小时前
MySQL 表的操作:从建表到删表,一篇讲清楚
android·mysql·oracle
事圆则缓7 小时前
Android 性能优化常见工具与使用场景:从症状到证据的排查指南
android·性能优化
avi91117 小时前
Unity 非后台,非自动构建,但统计报表 BuildReport ,构建自动化系统C#
android·webserver·ios打包·admin·c#后台·unity打包系统·unity自动构建
新鲜势力呀8 小时前
PHP 定时任务系统实战:从 Cron 混乱执行到任务调度中心 + Redis队列 + 失败重试
android·java·redis
YF02118 小时前
Android 16系统 如何修改 eth0 IP ?
android
其实防守也摸鱼9 小时前
渗透测试学习计划(全栈综合 · 零基础进阶)
android·数据库·学习·安全·oracle·自动化·学习方法
释厄6239 小时前
BSD 简单真理循环论——简单真理 × 复杂循环=大一统
android·开发语言·kotlin