Android获取手机内存和sd卡相关信息

1、手机内存已用和可用空间

 1 private String[] getPhoneMemory() {   
 2         String[] result = {"",""};  //1-total 2-avail   
 3         ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();    
 4         ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
 5 
 6         am.getMemoryInfo(mi);     
 7         long mTotalMem = 0;   
 8         long mAvailMem = mi.availMem;   
 9         String str1 = "/proc/meminfo";   
10         String str2;   
11         String[] arrayOfString;   
12         try {   
13             FileReader localFileReader = new FileReader(str1);   
14             BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);   
15             str2 = localBufferedReader.readLine();   
16             arrayOfString = str2.split("\s+");   
17             mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;   
18             localBufferedReader.close();   
19         } catch (IOException e) {   
20             e.printStackTrace();   
21         }   
22         result[0] = Formatter.formatFileSize(this, mTotalMem);   
23         result[1] = Formatter.formatFileSize(this, mAvailMem);   
25         return result;    
26 }  

2、sd卡已用和可用空间

1 public long getAvailaleSize() {// sd卡信息(可用)
2         File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径
3         StatFs stat = new StatFs(path.getPath());
4         long blockSize = stat.getBlockSize();
5         long availableBlocks = stat.getAvailableBlocks();
6         return availableBlocks * blockSize/1024 /1024;
9     }
1     public long getAllSize() {//总大小
2         File path = Environment.getExternalStorageDirectory();
3         StatFs stat = new StatFs(path.getPath());
4         long blockSize = stat.getBlockSize();
5         long availableBlocks = stat.getBlockCount();
6         return availableBlocks * blockSize/1024 /1024;
7     }
原文地址:https://www.cnblogs.com/snowspace/p/3297813.html