Android获得内部/外部内存的空闲大小我希望以编程方式在设备的内部/外部存储上获得空闲内存的大小。我在使用这段代码:StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;Log.e("","Available MB : "+megAvailable);
File path = Environment.getDataDirectory();StatFs stat2 = new StatFs(path.getPath());
long blockSize = stat2.getBlockSize();long availableBlocks = stat2.getAvailableBlocks();
String format = Formatter.formatFileSize(this, availableBlocks * blockSize);Log.e("","Format : "+format);我得到的结果是:11-15 10:27:18.844: E/(25822): Available MB : 757211-15 10:27:18.844: E/(25822): Format : 869MB问题是,我想获得sdCard的空闲内存,这是1,96GB现在就来。我怎样才能修正这段代码,才能得到自由大小呢?
3 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());long bytesAvailable;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();}else { bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();}long megAvailable = bytesAvailable / (1024 * 1024); Log.e("","Available MB : "+megAvailable);
- 3 回答
- 0 关注
- 294 浏览
添加回答
举报
0/150
提交
取消