Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get free space on internal memory

Is it possible to get the amount of free memory on an Android device (not on the SD CARD) via the Android SDK?

If so, how?

like image 754
clamp Avatar asked Jan 04 '11 15:01

clamp


People also ask

What do I do if my internal storage is full?

In the app's Application info menu, tap Storage and then tap Clear Cache to clear the app's cache. To clear cached data from all apps, go to Settings > Storage and tap Cached data to clear the caches of all the apps on your phone.

How do I manage internal storage on Android?

Open the Settings app. Tap General, then tap Usage. In the Usage screen, tap Manage Storage and wait a few seconds for a list of apps and their storage usage to appear. If you see the > icon to the right of an app name, you can delete its files.


2 Answers

this post might fit well to your question.

also check this thread. there is so much info here on SO.

googled a bit and here is the solution (found at android git)

File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return Formatter.formatFileSize(this, availableBlocks * blockSize); 
like image 82
mad Avatar answered Oct 16 '22 04:10

mad


/************************************************************************************************* Returns size in bytes.  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 long TotalMemory()     {         StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());            long   Total  = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize());         return Total;     }      public long FreeMemory()     {         StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());         long   Free   = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize());         return Free;     }      public long BusyMemory()     {         StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());            long   Total  = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());         long   Free   = (statFs.getAvailableBlocks()   * (long) statFs.getBlockSize());         long   Busy   = Total - Free;         return Busy;     } 

Converting bytes to human readable format (like 1 Mb, 1 Gb)

    public static String floatForm (double d)     {        return new DecimalFormat("#.##").format(d);     }       public static String bytesToHuman (long size)     {         long Kb = 1  * 1024;         long Mb = Kb * 1024;         long Gb = Mb * 1024;         long Tb = Gb * 1024;         long Pb = Tb * 1024;         long Eb = Pb * 1024;          if (size <  Kb)                 return floatForm(        size     ) + " byte";         if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";         if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";         if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";         if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";         if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";         if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";          return "???";     } 
like image 35
XXX Avatar answered Oct 16 '22 06:10

XXX