获取SD卡大小和ROM大小

package com.urovo.sdcardspace;

import java.io.File;

import android.os.Bundle;

import android.os.Environment;

import android.os.StatFs;

import android.app.Activity;

import android.text.format.Formatter;

import android.view.Menu;

import android.widget.TextView;

public class MainActivity extends Activity {

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    TextView tv = (TextView) findViewById(R.id.tv);

    File path = Environment.getExternalStorageDirectory();//得到SD卡的路径

    StatFs stat = new StatFs(path.getPath());//创建StatFs对象,用来获取文件系统的状态

    long blockCount = stat.getBlockCount();

    long blockSize = stat.getBlockSize();

    long availableBlocks = stat.getAvailableBlocks();

    String totalSize = Formatter.formatFileSize(getApplicationContext(), blockCount*blockSize);//格式化获得SD卡总容量

    String availableSize = Formatter.formatFileSize(getApplicationContext(), blockCount*availableBlocks);//获得SD卡可用容量

    tv.setText("SD卡总容量:"+totalSize+"\nSD卡可用容量:"+availableSize+"\n"+getRomSpace());

  }

  private String getRomSpace() {

    File path = Environment.getDataDirectory();

    StatFs stat = new StatFs(path.getPath());

    long blockCount = stat.getBlockCount();

    long blockSize = stat.getBlockSize();

    long availableBlocks = stat.getAvailableBlocks();

    String totalSize = Formatter.formatFileSize(getApplicationContext(), blockCount*blockSize);

    String availableSize = Formatter.formatFileSize(getApplicationContext(), blockCount*availableBlocks);

    return "手机Rom总容量:"+totalSize+"\n手机Rom可用容量:"+availableSize;

  }

}

/** 
* 获得SD卡总大小 
* 
* @return 
*/
private String getSDTotalSize() { 
  File path = Environment.getExternalStorageDirectory(); 
  StatFs stat = new StatFs(path.getPath()); 
  long blockSize = stat.getBlockSize(); 
  long totalBlocks = stat.getBlockCount(); 
  return Formatter.formatFileSize(MainActivity.this, blockSize * totalBlocks); 
} 
/** 
* 获得sd卡剩余容量,即可用大小 
* 
* @return 
*/
private String getSDAvailableSize() { 
  File path = Environment.getExternalStorageDirectory(); 
  StatFs stat = new StatFs(path.getPath()); 
  long blockSize = stat.getBlockSize(); 
  long availableBlocks = stat.getAvailableBlocks(); 
  return Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); 
} 
/** 
* 获得机身内存总大小 
* 
* @return 
*/
private String getRomTotalSize() { 
  File path = Environment.getDataDirectory(); 
  StatFs stat = new StatFs(path.getPath()); 
  long blockSize = stat.getBlockSize(); 
  long totalBlocks = stat.getBlockCount(); 
  return Formatter.formatFileSize(MainActivity.this, blockSize * totalBlocks); 
} 
/** 
* 获得机身可用内存 
* 
* @return 
*/
private String getRomAvailableSize() { 
  File path = Environment.getDataDirectory(); 
  StatFs stat = new StatFs(path.getPath()); 
  long blockSize = stat.getBlockSize(); 
  long availableBlocks = stat.getAvailableBlocks(); 
  return Formatter.formatFileSize(MainActivity.this, blockSize * availableBlocks); 
}

 

你可能感兴趣的:(获取SD卡大小和ROM大小)