/文章

【摘】Android:获取内部存储信息方法总结

文章目录

前言

Android开发中,需要获取磁盘的大小信息。

项目中也有获取磁盘信息的方法,但只有一种,因此摘抄了网上的 《Android : 获取内部存储信息方法总结

正文

方法一
private long getSDCardAvailSize() {    
    String state = Environment.getExternalStorageState();  
    long aaa=0;    
    if (Environment.MEDIA_MOUNTED.equals(state)) {      
        File sdcardDir = Environment.getExternalStorageDirectory();        
        StatFs sf = new StatFs(sdcardDir.getPath());     
        long blockSize = sf.getBlockSize(); 
        long blockCount = sf.getBlockCount();      
        long availCount = sf.getAvailableBlocks();     
        long totalSeize = blockSize * blockCount;     
        aaa = availCount * blockSize;      
        Log.e("size", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize * blockCount / 1024 + "KB");  
        Log.e("size", "可用的block数目::" + availCount + ",剩余空间:" + availCount * blockSize / 1024 + "KB"); 
    }   
    return aaa;
}
方法二
public long getSystemAvailableStorage() throws RemoteException {
        try {
            StorageStatsManager stats = mContext.getSystemService(StorageStatsManager.class);
            return stats.getFreeBytes(StorageManager.UUID_DEFAULT)/1024/1024;
        } catch (NoSuchFieldError | NoClassDefFoundError | NullPointerException | IOException e) {
            e.printStackTrace();
            return -1;
        }
    }
方法三
//获取可用存储大小,data区的剩余空间,返回字节数public long getAvailableInternalSize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

参考文章

  1. Android : 获取内部存储信息方法总结
发表评论