天天看點

Android 擷取手機本機記憶體、SD卡記憶體使用情況

Android 擷取手機本機記憶體、SD卡記憶體使用情況

  /**
     * 獲得SD卡總大小
     * 
     * @return
     */
    public Long getSDTotalSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return blockSize * totalBlocks;
    }

    /**
     * 獲得sd卡剩餘容量,即可用大小
     * 
     * @return
     */
    public long getSDAvailableSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return blockSize * availableBlocks;
    }

    /**
     * 獲得機身記憶體總大小
     * 
     * @return
     */
    public long getRomTotalSize() {
        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();// 讀取meminfo第一行,系統總記憶體大小

            arrayOfString = str2.split("\\s+");
            for (String num : arrayOfString) {
                Log.i(str2, num + "\t");
            }

            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統總記憶體,機關是KB,乘以1024轉換為Byte
            localBufferedReader.close();

        } catch (IOException e) {
        }
        return initial_memory;
    }

    /**
     * 獲得機身可用記憶體
     * 
     * @return
     */
    public long getRomAvailableSize() {
        ActivityManager am = (ActivityManager) Context.getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem;
    }