Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

android check free space programmatically

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

    public static String getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlocks = stat.getAvailableBlocksLong();
        return formatSize(availableBlocks * blockSize);
    }

    public static String getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return formatSize(totalBlocks * blockSize);
    }

    public static String getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSizeLong();
            long availableBlocks = stat.getAvailableBlocksLong();
            return formatSize(availableBlocks * blockSize);
        } else {
            return ERROR;
        }
    }

    public static String getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSizeLong();
            long totalBlocks = stat.getBlockCountLong();
            return formatSize(totalBlocks * blockSize);
        } else {
            return ERROR;
        }
    }

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

android check free space programmatically

//gets available ram space during runtime
ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;
Comment

PREVIOUS NEXT
Code Example
Java :: new thrad java 
Java :: java generics 
Java :: generate random number in java within a range without repeating with android studio 
Java :: Create JDBC connection using properties file 
Java :: hdfs java read all files in directory 
Java :: hide background of button java swing 
Java :: how to set path for java in windows 10 
Java :: android text change java debounce 
Java :: how to convert character into operator 
Java :: get current background color of TextView java 
Java :: spring data sql not utf8 
Java :: Java Enabling Assertions 
Java :: how to create a subclass in java 
Java :: how to convert string hashcode to color in java android 
Java :: android java show hide keyboard in AndroidManifest 
Java :: Explain JDK, JRE and JVM? 
Java :: convert arraylist of integers to array primitive 
Java :: Sample HashMap 
Java :: put arraylist in hashtable java 
Java :: what is delegation in java 
Java :: java.lang.NoClassDefFoundError 
Java :: HashMap to store key and value pair. 
Java :: kotlin edittext default value 
Java :: find out duplicate number between 1 to n numbers in java 
Java :: search 
Java :: enhanced for loop arraylist 
Java :: Java void Keyword 
Java :: random suffling java 
Java :: JAVA Multi-line Comment 
Java :: java garbage collection 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =