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;
}
相关推荐
美狐美颜SDK开放平台8 小时前
准备开发直播APP?视频美颜SDK接入前的十项检查清单
android·ios·音视频·直播美颜sdk·视频美颜sdk
传奇开心果编程8 小时前
【Jetpack Compose娓娓道来】第26课:Repository模式与数据层设计——数据的唯一入口
android·移动开发·1024程序员节·jetpack compose·编程语言理论·编程框架·声明式ui
三少爷的鞋13 小时前
Kotlin 协程,重点就在「协」这个字上
android·kotlin
好的佩奇13 小时前
Dart 之模式
android·flutter·dart
淡淡的香烟15 小时前
Flutter实现多语言切换
android·flutter·1024程序员节
AIsoft_868917 小时前
手机视频转文字工具清单:哪些APP支持录屏识别
android·音视频
_祝你今天愉快19 小时前
Paint Cavans高级绘制:[一]Paint画笔高级应用
android
@zulnger20 小时前
安卓进行稳定性测试
android·appium·1024程序员节
不吃凉粉21 小时前
android 副屏连接
android
OxYGC1 天前
手机为什么能“自动跳过“开屏广告?李跳跳、GKD 与 iOS 代理软件原理盘点
android·ios·开屏广告·广告屏蔽