Tuesday, April 10, 2012

Get Available Internal Memory

The below snippet helps to find the available internal memory. It will return the result in MB.

/**
       * Calculates the available internal memory
       * @return internal memory available in float
       */
      private float getAvailableInternalMemory()
      {
              float availMB;
              File path ;
              StatFs stat;       
              long blockSize;           
              long availableBlocks;
              try
              {
                  path = Environment.getDataDirectory();
                  stat = new StatFs(path.getPath());
                  blockSize = stat.getBlockSize();
                  availableBlocks = stat.getAvailableBlocks();
                  availMB = (availableBlocks*blockSize)/(1024*1024);               
              }
              catch (Exception e)
              {
                  availMB = -1;
              }         
             
              return availMB;
      }