Android中我们经常会使用android.os.StatFs来获取关于文件系统空间的状态信息。
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
long blockSize = sf.getBlockSize();
long blockCount = sf.getBlockCountLong();
long availCount = sf.getAvailableBlocksLong();
Log.d("", "block大小:"+ blockSize+",block数目:"+ blockCount+",总大小:"+blockSize*blockCount/1024+"KB");
Log.d("", "可用的block数目::"+ availCount+",剩余空间:"+ availCount*blockSize/1024+"KB");
但上面的方法我们会发现剩余空间和df命令显示的Free大小不一样,这是怎么回事呢?