Android系统信息获取之一:系统存储信息的获取(RAM,ROM,SDCard)
Android系统的存储设备一般分为RAM,ROM,SDCard三个部分。其中RAM是Random Access Memory的缩写,是随机存储器,在工作状态时可以随机读写数据,断电以后会丢失数据,即我们常说的内存。手机的ROM和传统的ROM(Read Only Memory)又有些不一样,它分为两部分,一部分是用于系统,另外一部分是用作用户存储数据。SDCard即为我们平时所说的存储卡,8G,16G等,常用的有TF卡,用于存储用户数据。
在Android系统中存储设备信息在开发中随处可见,这里将开发中获取这些存储设备信息的方法进行总结。
一、内存(RAM)信息获取:
android的总内存大小信息存放在系统的/proc/meminfo文件里面,可以通过读取这个文件来获取这些信息:
public void getMemoryInfo() {
String str1 = "/proc/meminfo";
String str2="";
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
Log.d(TAG, "---" + str2);
}
} catch (IOException e) {
}
}
运行信息如下:
08-02 15:29:06.962: D/StorgeState(15471): ---MemTotal: 516452 kB
08-02 15:29:06.972: D/StorgeState(15471): ---MemFree: 255280 kB
08-02 15:29:06.972: D/StorgeState(15471): ---Buffers: 1056 kB
08-02 15:29:06.982: D/StorgeState(15471): ---Cached: 106184 kB
08-02 15:29:06.982: D/StorgeState(15471): ---SwapCached: 0 kB
08-02 15:29:06.982: D/StorgeState(15471): ---Active: 178084 kB
08-02 15:29:06.982: D/StorgeState(15471): ---Inactive: 63172 kB
08-02 15:29:06.992: D/StorgeState(15471): ---Active(anon): 146004 kB
08-02 15:29:06.992: D/StorgeState(15471): ---Inactive(anon): 0 kB
08-02 15:29:07.002: D/StorgeState(15471): ---Active(file): 32080 kB
08-02 15:29:07.002: D/StorgeState(15471): ---Inactive(file): 63172 kB
08-02 15:29:07.002: D/StorgeState(15471): ---Unevictable: 0 kB
08-02 15:29:07.002: D/StorgeState(15471): ---Mlocked: 0 kB
08-02 15:29:07.002: D/StorgeState(15471): ---SwapTotal: 0 kB
08-02 15:29:07.002: D/StorgeState(15471): ---SwapFree: 0 kB
08-02 15:29:07.002: D/StorgeState(15471): ---Dirty: 0 kB
08-02 15:29:07.014: D/StorgeState(15471): ---Writeback: 0 kB
08-02 15:29:07.014: D/StorgeState(15471): ---AnonPages: 134040 kB
08-02 15:29:07.014: D/StorgeState(15471): ---Mapped: 46600 kB
08-02 15:29:07.014: D/StorgeState(15471): ---Slab: 5532 kB
08-02 15:29:07.014: D/StorgeState(15471): ---SReclaimable: 2212 kB
08-02 15:29:07.022: D/StorgeState(15471): ---SUnreclaim: 3320 kB
08-02 15:29:07.022: D/StorgeState(15471): ---PageTables: 7356 kB
08-02 15:29:07.032: D/StorgeState(15471): ---NFS_Unstable: 0 kB
08-02 15:29:07.032: D/StorgeState(15471): ---Bounce: 0 kB
08-02 15:29:07.032: D/StorgeState(15471): ---WritebackTmp: 0 kB
08-02 15:29:07.032: D/StorgeState(15471): ---CommitLimit: 258224 kB
08-02 15:29:07.032: D/StorgeState(15471): ---Committed_AS: 1199832 kB
08-02 15:29:07.032: D/StorgeState(15471): ---VmallocTotal: 450560 kB
08-02 15:29:07.032: D/StorgeState(15471): ---VmallocUsed: 23616 kB
08-02 15:29:07.042: D/StorgeState(15471): ---VmallocChunk: 403460 kB
根据每一项的名称,很容易理解所对应的信息。
我们通过查看“/proc/meminfo”内容获取更内存的详细信息。有时候我们只是需要知道还有多少可用内存就不需要读取出如此多的内存信息,通过读取“/proc/meminfo”要把数据分离出来还是相当麻烦的,在这种情况下,我们可以通过Android系统提供的ActivityManager对象获取其可用的内存。
获取当前剩余内存(ram)大小的方法:
/**
* 获取可用手机内存(RAM)
* @return
*/
public static long getAvailMemory(Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}
二、ROM信息获取
/**
* 获取手机内部空间大小
* @return
*/
public static long getTotalInternalStorgeSize() {
File path = Environment.getDataDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long totalBlocks = mStatFs.getBlockCount();
return totalBlocks * blockSize;
}
/**
* 获取手机内部可用空间大小
* @return
*/
public static long getAvailableInternalStorgeSize() {
File path = Environment.getDataDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long availableBlocks = mStatFs.getAvailableBlocks();
return availableBlocks * blockSize;
}
说明:
1、 注意类型,不然相乘之后会有溢出。
2、 内部存储(ROM )的信息要通过getDataDirectory()取得。
三、存储卡(SDCard)信息获取
/**
* 获取手机外部空间大小
* @return
*/
public static long getTotalExternalStorgeSize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long totalBlocks = mStatFs.getBlockCount();
return totalBlocks * blockSize;
} else {
return ERROR;
}
}
/**
* 获取手机外部可用空间大小
* @return
*/
public static long getAvailableExternalStorgeSize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs mStatFs = new StatFs(path.getPath());
long blockSize = mStatFs.getBlockSize();
long availableBlocks = mStatFs.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return ERROR;
}
}
说明:注意类型,不然相乘之后会有溢出。
四、两个工具
通过获取存储信息读出来存储空间大小数据单位是Bit,看起来并不直观和人性化,因此需要对这些数据进行格式化。格式化数据方法如下:
/* 返回为字符串数组[0]为大小[1]为单位KB或MB */
public static String formatSize(long size) {
String suffix = "";
float fSzie = 0;
if (size >= 1024) {
suffix = "KB";
fSzie = size / 1024;
if (fSzie >= 1024) {
suffix = "MB";
fSzie /= 1024;
if (fSzie >= 1024) {
suffix = "GB";
fSzie /= 1024;
}
}
}
DecimalFormat formatter = new DecimalFormat("#0.00");// 字符显示格式
/* 每3个数字用,分隔,如1,000 */
formatter.setGroupingSize(3);
StringBuilder resultBuffer = new StringBuilder(formatter.format(fSzie));
if (suffix != null) {
resultBuffer.append(suffix);
}
return resultBuffer.toString();
}
另外一个工具:判断外部存储(SDCard)是否可用方法:
/**
* 外部存储(SDCard)是否可用
* @return
*/
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
五、小结:
这里所有的方法都使用了static进行声明,原因是获取系统这些信息的方法是固定而且是比较常用的。我是单独把这些方法分类作为工具类进行了整理。以备以后使用,而不用每次都要去重新写这些方法,虽然花费不了多少时间,但是良好的习惯是提高效率的很重要一环。