Search code examples
androidstorageandroid-sdcardremovable

how to get the internal sdcard storage and External(removal) sdcard storage and system storage and available memory status in android?


I'm new to Android. I need the simple code of getting the Internal storage and external storage and System storage and how to get the Details of available memory(internal and external) space, total memory space. My code is below but its getting error in "StatFs" method. Thanks in advance.

    long total, aval,total1, aval1,total2, aval2;
    int kb = 1024;

    StatFs fs = new StatFs(Environment.
                            getExternalStorageDirectory().getPath());

    total = fs.getBlockCount() * (fs.getBlockSize() / kb);
    aval = fs.getAvailableBlocks() * (fs.getBlockSize() / kb);
    //Here Iam Getting error StatFs method not loading
    StatFs fs1 = new StatFs(Environment.
    getRootDirectory()+"/storage/extSdCard/");


 total1 = fs1.getBlockCount() * (fs1.getBlockSize() / kb);
 aval1 = fs1.getAvailableBlocks() * (fs1.getBlockSize() / kb);

    pb1.setMax((int)total);
    pb1.setProgress((int)aval);
    pb2.setMax((int)total1);
    pb2.setProgress((int)aval1);

}

Solution

  • When accessing the SDCARD services Always start by checking whether or not the SDCARD is currently mounted. You cannot assume that it is:

    String state = Environment.getExternalStorageState();
    
    if (state.equals(android.os.Environment.MEDIA_MOUNTED)) {
           // is mounted, can continue and check size
    
            StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
            long AvailableBytes = (long)stat.getBlockSize() *(long)stat.getBlockCount();
            long AvailableInMegas = AvailableBytes / 1048576; // <------------------
    }
    


    Now, to get available internal storage:

    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    long availableInternalInBytes = formatSize(availableBlocks * blockSize);
    

    Note: the above will return available storage for all, not only for your app!


    Get memory usage:

    final Runtime runtime = Runtime.getRuntime();
    final long totalMem = runtime.totalMemory(); // <--------- total for YOUR JVM!
    final long freeMem = runtime.freeMemory();  // <--------- free in YOUR JVM!
    final long usedMem = totalMem - freeMem; // <--------- used in YOUR JVM!
    final long maxMem = runtime.maxMemory()  // <--------- the max amount of mem that YOUR JVM may attempt to use
    

    All values are in bytes.


    And finally - interrogating the external memory (not the SDCARD):

    You code assumes that the path to the above is "/mnt/extSdCard/". This is not guaranteed. In some devices it is "/mnt/external_sd/". And there can be other names..

    What you need to do is to start by listing all mounted storage devices and somehow (programmatically, user intervention...) pick your one. The way to do it is as follows:

    File mountedRoot = new File("/mnt/");
    if(mountedRoot.isDirectory()) {
        String[] allMountedFolders = storageDir.list();
        if (allMountedFolders != null) {
             for (String f: allMountedFolders) {
                   // iterate over all mounted devices <-----------------------
             }
        }
    
    }