使用这个使用StatFs类的辅助类:
public class MemoryUsage{
/*
Returns size in MegaBytes.
*/
public int TotalMemory()
{
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
return Total;
}
public int FreeMemory()
{
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
return Free;
}
public int BusyMemory()
{
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
int Busy = Total - Free;
return Busy;
}
}
如果您需要计算内部内存,请更改此:
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
对此:
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());