• Android之获取sdcard卡的信息


        public static SDCardInfo getSDCardInfo() {
            String sDcString = android.os.Environment.getExternalStorageState();
    
            if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
                File pathFile = android.os.Environment.getExternalStorageDirectory();
    
                try {
                    android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
    
                    // 获取SDCard上BLOCK总数
                    long nTotalBlocks = statfs.getBlockCount();
    
                    // 获取SDCard上每个block的SIZE
                    long nBlocSize = statfs.getBlockSize();
    
                    // 获取可供程序使用的Block的数量
                    long nAvailaBlock = statfs.getAvailableBlocks();
    
                    // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
                    long nFreeBlock = statfs.getFreeBlocks();
    
                    SDCardInfo info = new SDCardInfo();
                    // 计算SDCard 总容量大小MB
                    info.total = nTotalBlocks * nBlocSize;
    
                    // 计算 SDCard 剩余大小MB
                    info.free = nAvailaBlock * nBlocSize;
    
                    return info;
                } catch (IllegalArgumentException e) {
                    Log.e(LOG_TAG, e.toString());
                }
            }
    
            return null;
        }
    SDCardInfo sdCardInfo = Util.getSDCardInfo();
    // sd卡总容量
    sdCardInfo.total
    // sd卡剩余容量
    sdCardInfo.free
  • 相关阅读:
    第一个Servlet项目(IDEA)
    Web交互基本流程以及HTTP协议详解
    mybatis中Mapper.xml配置详解
    认识mybatis
    SpringAOP
    Spring AOP
    70. Climbing Stairs
    位运算
    Leetcode分类
    21. Merge Two Sorted Lists
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3393482.html
Copyright © 2020-2023  润新知