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;
}
相关推荐
落落落sss11 分钟前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
代码敲上天.32 分钟前
数据库语句优化
android·数据库·adb
GEEKVIP3 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model20055 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏6895 小时前
Android广播
android·java·开发语言
与衫6 小时前
掌握嵌套子查询:复杂 SQL 中 * 列的准确表列关系
android·javascript·sql
500了12 小时前
Kotlin基本知识
android·开发语言·kotlin
人工智能的苟富贵13 小时前
Android Debug Bridge(ADB)完全指南
android·adb
小雨cc5566ru18 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
bianshaopeng19 小时前
android 原生加载pdf
android·pdf