标签:
1 package com.example.sdcardspace; 2 3 import java.io.File; 4 5 import android.os.Bundle; 6 import android.os.Environment; 7 import android.os.StatFs; 8 import android.app.Activity; 9 import android.text.format.Formatter; 10 import android.view.Menu; 11 import android.widget.TextView; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 TextView tv = (TextView)findViewById(R.id.tv); 20 21 File path = Environment.getExternalStorageDirectory(); 22 StatFs stat = new StatFs(path.getPath()); 23 long blockSize = stat.getBlockSize(); 24 long totalBlocks = stat.getBlockCount(); 25 long availblocks = stat.getAvailableBlocks(); 26 long totalSize = blockSize * totalBlocks; 27 long availSize = blockSize * availblocks; 28 String totalStr = Formatter.formatFileSize(this, totalSize); 29 String availStr = Formatter.formatFileSize(this, availSize); 30 31 tv.setText("总空间为: " + totalStr + "\n可用空间为: " + availStr + "\n" + getMonmery()); 32 33 34 } 35 36 public String getMonmery() 37 { 38 File path = Environment.getDataDirectory(); 39 StatFs stat = new StatFs(path.getPath()); 40 long blockSize = stat.getBlockSize(); 41 long totalBlocks = stat.getBlockCount(); 42 long availblocks = stat.getAvailableBlocks(); 43 long totalSize = blockSize * totalBlocks; 44 long availSize = blockSize * availblocks; 45 String totalStr = Formatter.formatFileSize(this, totalSize); 46 String availStr = Formatter.formatFileSize(this, availSize); 47 return "内存块的总容量为: " + totalStr + "\n可用空间为: " + availStr; 48 } 49 50 }
标签:
原文地址:http://www.cnblogs.com/zhongyinghe/p/5386503.html