Open top menu


Explain:-How to Convert Memory size in MB & KB  Format ?


In this tutorial implement conversion size in MB & KB format parameter type String method, below code . and explain how to Pass External And Internal Memory size  in this method..


// get Internal memory size...

public static String getTotalInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return formatSize(totalBlocks * blockSize);// pass memory size in this method
}








// format Convert Method.......
public static String formatSize(long size) {
    String suffixSize = null;

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

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

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

    if (suffixSize != null) BufferSize.append(suffixSize);
    return BufferSize.toString();
}





Tagged

0 comments