Android 获取机身内存,SD卡内存,Ram内存。

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.csdn.net/loongggdroid/article/details/12304695


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值