Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the amount of free storage (disk space) left on Android? [duplicate]

I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?

Thanks,

like image 373
Ashwin Avatar asked Aug 18 '11 22:08

Ashwin


People also ask

How many storage do I have left?

By navigating to your Android device's Settings app and clicking on the Storage option, you'll be able to look at an at-a-glance view of your storage. Up top, you'll see how much of your phone's total storage you're using, followed by a breakdown of different categories that use up space on your phone.

What does free storage mean?

Use Android's “Free up space” tool. Android has a built-in tool to help you increase the amount of useable storage on your phone. It's easy to find: Go to your phone's settings, and select “Storage.” You'll see information on how much space is in use and a list of file categories. Tap on the “Free up space” button.


1 Answers

Example: Getting human readable size like 1 Gb

String memory = bytesToHuman(totalMemory())

/************************************************************************************************* 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  = (statFs.getBlockCount() * statFs.getBlockSize());         return total;     }      public long freeMemory()     {         StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());         long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());         return free;     }      public long busyMemory()     {         StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());            long   total  = (statFs.getBlockCount() * statFs.getBlockSize());         long   free   = (statFs.getAvailableBlocks() * 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 130
XXX Avatar answered Oct 13 '22 23:10

XXX