try { //判断Sdcard是否可用
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
file = new File(context.getCacheDir(),"info.txt");
//file = new File(context.getFileDir(),"info.txt");
fos = new FileOutputStream(file);
fos.close();
} else {
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
查看Android手机Sd卡的储存空间
TextView tv = (TextView) this.findViewById(R.id.sdcardSize);
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlock = stat.getAvailableBlocks();
long totalSize = blockSize * totalBlocks;
long availSize = availableBlock * blockSize;
String totalStr = Formatter.formatFileSize(this, totalSize);
String availStr = Formatter.formatFileSize(this, availSize);
tv.setText("总容量:"+ totalStr + "\n" + "可用内存:" + availStr);