获取sd卡的内存空间和可用空间

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  
    //或sd的路径
    File file=Environment.getExternalStorageDirectory();
    //获取sd卡的所有信息
    StatFs fs=new StatFs(file.getPath());
    //获取每一个区块的大小 Android4.3以上支持getBlockSizeLong方法
    long blocksize=fs.getBlockSize();
    //所有区块数量 Android4.3以上支持getBlockCount方法
    long totalBlocku=fs.getBlockCount();
    //可用区块的数量 Android4.3以上支持getAvailableBlocks方法
    long availableBlocku=fs.getAvailableBlocks();  
  
    //可用区块总大小乘于区块大小
    String text=formutSize(availableBlocku*blocksize);
  
    TextView tv=(TextView) findViewById(R.id.textView1);
    tv.setText(text);
   }
 
   //做类型转换的方法
   private String formutSize(long size){
    return Formatter.formatFileSize(this, size);
 }

posted @ 2015-11-08 18:25  随笔、  阅读(273)  评论(0编辑  收藏  举报