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;
}
相关推荐
诸神黄昏EX4 小时前
Android Build系列专题【篇四:编译相关语法】
android
雨白7 小时前
优雅地处理协程:取消机制深度剖析
android·kotlin
leon_zeng07 小时前
更改 Android 应用 ID (ApplicationId) 后遭遇记
android·发布
2501_916007479 小时前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
Jeled10 小时前
Retrofit 与 OkHttp 全面解析与实战使用(含封装示例)
android·okhttp·android studio·retrofit
ii_best13 小时前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
2501_9159090613 小时前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
limingade14 小时前
手机转SIP-手机做中继网关-落地线路对接软交换呼叫中心
android·智能手机·手机转sip·手机做sip中继网关·sip中继
RainbowC014 小时前
GapBuffer高效标记管理算法
android·算法
程序员码歌15 小时前
豆包Seedream4.0深度体验:p图美化与文生图创作
android·前端·后端