Open top menu


Explain:-How to get Internal and External Memory Size Programmatically in Android Device ?

explain step by step....

1. you can check  internal & external memory is exist or not .


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


2. get Internal memory .


 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);
}


3.get External memory .

public static String getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.
                getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long BlockSize = stat.getBlockSize();
        long TotalBlocks = stat.getBlockCount();
        return formatSize(TotalBlocks * BlockSize);
    } else {
        return ERROR;
    }
}


4. Convert Memory  size MB & KB Format.

 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