Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all the total and available space on Android

To my knowledge, on Android, there is:

  • Internal memory for apps and cache
  • Internal SD Card on some phones (not removable)
  • External SD Card for music and photos (removable)

How do I get the total and available for each with a check that they exist (some phones do not have an internal SD Card)

Thanks

like image 387
zsniperx Avatar asked Jan 25 '11 22:01

zsniperx


2 Answers

Here is how you get internal storage sizes:

 StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

Here is how you get external storage sizes (SD card size):

 StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

Short note

Free blocks:

The total number of blocks that are free on the file system, including reserved blocks (that are not available to normal applications).

Available blocks:

The number of blocks that are free on the file system and available to applications.


Here is how to detect whether SD card is mounted:

 String state = Environment.getExternalStorageState();
 if (Environment.MEDIA_MOUNTED.equals(state)) 
 {
   // We can read and write the media    
 } 
 else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
 {
    // We can only read the media     
 } 
 else 
 {
    // No external media
 }
like image 78
inazaruk Avatar answered Sep 18 '22 09:09

inazaruk


/*************************************************************************************************
Returns size in MegaBytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public int TotalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        return Total;
    }

    public int FreeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        int Free  = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        return Free;
    }

    public int BusyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        int Free  = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        int Busy  = Total - Free;
        return Busy;
    }
like image 22
XXX Avatar answered Sep 18 '22 09:09

XXX