get Total Memory Size - Android Hardware

Android examples for Hardware:Memory

Description

get Total Memory Size

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static long getmem_TOLAL() {
        long mTotal;
        // /*from   w w w .j  av a 2 s .  c om*/
        String path = "/proc/meminfo";
        // 
        String content = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(path), 8);
            String line;
            if ((line = br.readLine()) != null) {
                // 
                content = line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // beginIndex
        int begin = content.indexOf(':');
        // endIndex
        int end = content.indexOf('k');
        // 
        content = content.substring(begin + 1, end).trim();
        // Int
        mTotal = Integer.parseInt(content);
        return mTotal;
    }

    private static String readLine(String filename) throws IOException {
        BufferedReader reader = new BufferedReader(
                new FileReader(filename), 256);
        try {
            return reader.readLine();
        } finally {
            reader.close();
        }
    }
}

Related Tutorials