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;
}
相关推荐
图王大胜1 小时前
Android Framework AMS(17)APP 异常Crash处理流程解读
android·app·异常处理·ams·crash·binderdied·讣告
ch_s_t1 小时前
电子商务网站之首页设计
android
豆 腐4 小时前
MySQL【四】
android·数据库·笔记·mysql
想取一个与众不同的名字好难6 小时前
android studio导入OpenCv并改造成.kts版本
android·ide·android studio
Jewel1057 小时前
Flutter代码混淆
android·flutter·ios
Yawesh_best8 小时前
MySQL(5)【数据类型 —— 字符串类型】
android·mysql·adb
曾经的三心草10 小时前
Mysql之约束与事件
android·数据库·mysql·事件·约束
guoruijun_2012_414 小时前
fastadmin多个表crud连表操作步骤
android·java·开发语言
Winston Wood14 小时前
一文了解Android中的AudioFlinger
android·音频
B.-16 小时前
Flutter 应用在真机上调试的流程
android·flutter·ios·xcode·android-studio