Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import android.os.Environment;
import android.os.StatFs;

import android.util.Log;

import java.io.File;

public class Main {
    private static final String TAG = "junu";

    public static void logExtMemInfo() {
        if (isExternalMemoryAvailable() == true) {
            jlog("MemoryInfo.extTotalMem : " + formatSize(getTotalExternalMemorySize()));
            jlog("MemoryInfo.extAvailMem : " + formatSize(getAvailableExternalMemorySize()));
        }
    }

    public static boolean isExternalMemoryAvailable() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }

    public static void jlog(String s) {
        Log.i(TAG, s);
    }

    public static void jlog(Object o) {
        Log.i(TAG, o.toString());
    }

    public static String formatSize(long size) {
        String suffix = null;

        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null) {
            resultBuffer.append(suffix);
        }

        return resultBuffer.toString();
    }

    public static long getTotalExternalMemorySize() {
        if (isExternalMemoryAvailable() == true) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = 0;
            long totalBlocks = 0;
            blockSize = stat.getBlockSize();
            totalBlocks = stat.getBlockCount();

            return totalBlocks * blockSize;
        } else {
            return -1;
        }
    }

    public static long getAvailableExternalMemorySize() {
        if (isExternalMemoryAvailable() == true) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = 0;
            long availableBlocks = 0;
            blockSize = stat.getBlockSize();
            availableBlocks = stat.getAvailableBlocks();

            return availableBlocks * blockSize;
        } else {
            return -1;
        }
    }
}