一:手机内存大小及可用内存
private String getAvailMemory() { ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); return Formatter.formatFileSize(getBaseContext(), mi.availMem); } private String getTotalMemory() { String str1 = "/proc/meminfo"; String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024; localBufferedReader.close(); } catch (IOException e) { } return Formatter.formatFileSize(getBaseContext(), initial_memory); }
二:SD卡的总大小及可用内存:
public float getSDAllSize(){ File path = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(path.getPath()); long blockSize = sf.getBlockSize(); long allBlocks = sf.getBlockCount(); return (allBlocks * blockSize)/1024/1024/1024; //单位MB } public float getSDFreeSize(){ File path = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(path.getPath()); long blockSize = sf.getBlockSize(); long freeBlocks = sf.getAvailableBlocks(); return (freeBlocks * blockSize)/1024/1024/1024; //单位MB }三:SD卡是否存在:
private boolean ExistSDCard() { if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { return true; }else{ return false; }
最后要在AndroidManifest.xml中增加SD卡访问权限
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 往SDCard读数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>