package com.example.tijiandemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.DecimalFormat;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.Toast;
public class GetMobileMemory {
Context context;
ActivityManager activityManager;
MemoryInfo info;
public GetMobileMemory(Context context){
this.context = context;
activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
info = new MemoryInfo();
activityManager.getMemoryInfo(info);//info.availMem; 当前系统的可用内存
}
public String getUsedRamMemory(){// 获取android当前可用ram内存大小
long used = info.totalMem - info.availMem ;
String usedRam = Formatter.formatFileSize(context, used);
return usedRam;
}
//获取总ram内存大小
public String getTotalRamMemory(){
long total = info.totalMem;
String totalRam = Formatter.formatFileSize(context, total);
return totalRam;
}
/*public String getTotalRamMemory(){//获得系统ram总内存
String sysMemory = "/proc/meminfo";//系统内存信息文件
String readFirst;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader fileReader = new FileReader(sysMemory);
BufferedReader bufferedReader = new BufferedReader(fileReader,8192);
readFirst = bufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
arrayOfString = readFirst.split("\\s+");
for (String string : arrayOfString) {
Log.i("Lain", string + "\t");
}
initial_memory = Integer.valueOf(arrayOfString[1]).intValue()*1024;// 获得系统rom总内存,单位是KB,乘以1024转换为Byte
bufferedReader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Formatter.formatFileSize(context, initial_memory);
}*/
//获取SD卡总大小
public String getSDCardSize() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
/* 获取block的SIZE */
long blockSize = stat.getBlockSize();
/* 块数量 */
long totalBlocks = stat.getBlockCount();
//计算SD卡的空间大小
long totalsize=blockSize*totalBlocks;
//转化为可以显示的字符串
String totalsize_str=Formatter.formatFileSize(context, totalsize);
/* 返回bit大小 */
return totalsize_str;
}
//获取SD卡已使用内存大小
public String getUsedSize() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
/* 获取block的SIZE */
long blockSize = stat.getBlockSize();
/* 块数量 */
long totalBlocks = stat.getBlockCount();
//可用数量块
long availableblocks=stat.getAvailableBlocks();
//计算SD卡的使用空间大小
long usedSize = blockSize*totalBlocks - availableblocks*blockSize;
//转化为可以显示的字符串
String usedsize_str=Formatter.formatFileSize(context, usedSize);
/* 返回已使用大小 */
return usedsize_str;
}
/**
* 获得机身内存总大小
*
* @return
*/
public String getRomTotalSize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
/**
* 获得机身已使用内存大小
*
* @return
*/
public String getRomUsedSize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();
long usedSize = blockSize * totalBlocks - blockSize * availableBlocks;
String usedsize_str=Formatter.formatFileSize(context, usedSize);
return usedsize_str;
}
}
转载请说明出处。
来源:http://blog.youkuaiyun.com/loongggdroid/article/details/12304695