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;
    }
相關文章
相關標籤/搜索