Androidはどのように携帯電話のメモリの大きさを取得して、内蔵のストレージの空間の大きさ


ダイレクトコード
package com.example.administrator.phoneinfo;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
        MemTotal:     RAM  。
        MemFree: LowFree HighFree   ,           。
        Buffers:           。
        Cached:         (cache memory)       (  diskcache minus SwapCache)。
        SwapCached:        (cache memory)         。          ,      swapfile ,                      I/O  。
        Active:                         ,      ,         。
        Inactive:                          ,         。
        SwapTotal:         。
        SwapFree:            。
        Dirty:              。
        Writeback:              。
        AnonPages:         。
        Mapped:            。
        Slab:            ,                。
        SReclaimable:   Slab   。
        SUnreclaim:    Slab   (SUnreclaim+SReclaimable=Slab)。
        PageTables:               。
        NFS_Unstable:        。
           android       ,    ”/proc/meminfo”    1 ,             。*/

/**
 * Created by Administrator on 2017/7/1.
 */
public class Tools {
    private static final String TAG = Tools.class.getName();
    private static final String MEM_INFO_PATH = "/proc/meminfo";
    public static final String MEMTOTAL = "MemTotal";
    public static final String MEMFREE = "MemFree";

    /**
     *        
     *
     * @param context
     * @param memtotal
     * @return
     */
    public static String getTotalMemory(Context context, String memtotal) {
        return getMemInfoIype(context, MEMTOTAL);
    }

    /**
     *         
     *
     * @param context
     * @param memfree
     * @return
     */
    public static String getMemoryFree(Context context, String memfree) {
        return getMemInfoIype(context, MEMFREE);
    }

    /**
     *   type info
     *
     * @param context
     * @param type
     * @return
     */
    public static String getMemInfoIype(Context context, String type) {
        try {
            FileReader fileReader = new FileReader(MEM_INFO_PATH);
            BufferedReader bufferedReader = new BufferedReader(fileReader, 4 * 1024);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                if (str.contains(type)) {
                    break;
                }
            }
            bufferedReader.close();
            /* \\s       ,  ,      ,
            +                */
            String[] array = str.split("\\s+");
            //        ,   KB,  1024   Byte
            int length = Integer.valueOf(array[1]).intValue() * 1024;
            return android.text.format.Formatter.formatFileSize(context, length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *             
     * @param context
     * @return
     */
    public static String getInternalToatalSpace(Context context){
        String path = Environment.getDataDirectory().getPath();
        Log.d(TAG,"root path is "+path);
        StatFs statFs = new StatFs(path);
        long blockSize = statFs.getBlockSize();
        long totalBlocks = statFs.getBlockCount();
        long availableBlocks = statFs.getAvailableBlocks();
        long useBlocks  = totalBlocks - availableBlocks;

        long rom_length = totalBlocks*blockSize;

        return Formatter.formatFileSize(context,rom_length);
    }
}