标签:
package com.example.getMem;
import java.io.File;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog.Builder;
public class MainActivity extends Activity {
private TextView tv_totalSize;
private TextView tv_availabelSize;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_totalSize=(TextView) findViewById(R.id.totalSize);
tv_availabelSize=(TextView) findViewById(R.id.availabel);
// 获取存储空间
File dataFileDir=Environment.getDataDirectory();
String data =getMemoryInfo(dataFileDir);
String []d= data.split("##");
tv_totalSize.setText("内部存储总大小:" + d[0]);
tv_availabelSize.setText("内部存储可用空间是:" + d[1]);
//获取Sd 卡空间
// File SdFileDir=Environment.getExternalStorageDirectory();
// //判断Sd卡是否可用
// if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
// String sdMemory =getMemoryInfo(SdFileDir);
// String []sd=sdMemory.split("##");
// tv_totalSize.setText("内部存储总大小:" + sd[0]);
// tv_availabelSize.setText("内部存储可用空间是:" + sd[1]);
//
// }
}
private String getMemoryInfo(File path) {
long blockSize;
long totalBlockCount;
long avaiLabelCount;
// TODO Auto-generated method stub
StatFs stat = new StatFs(path.getPath());
//检测系统版本
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.JELLY_BEAN_MR2){
//获取每个扇区的大小
blockSize = stat.getBlockSizeLong();
//获取总共有多少扇区
totalBlockCount=stat.getBlockCountLong();
//获取可用扇区数量
avaiLabelCount=stat.getAvailableBlocksLong();
}else{
blockSize=stat.getBlockSize();
totalBlockCount=stat.getBlockCount();
avaiLabelCount=stat.getAvailableBlocks();
}
// 磁盘总大小
String totalMemory =Formatter.formatFileSize(this, blockSize*totalBlockCount);
// 可用大小
String availabelMemory=Formatter.formatFileSize(this, blockSize*avaiLabelCount);
return totalMemory+"##"+availabelMemory;
}
}
http://pan.baidu.com/s/1bp0V87d
标签:
原文地址:http://www.cnblogs.com/XingzhiDai/p/5467101.html