zsniperx
zsniperx

Reputation: 2742

Getting all the total and available space on Android

To my knowledge, on Android, there is:

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

Upvotes: 12

Views: 11251

Answers (5)

Mark Martin
Mark Martin

Reputation: 372

StatFs - Retrieve overall information about the space on a filesystem. This is a wrapper for Unix statvfs().

From API level 18 use

long getTotalBytes - The total number of bytes supported by the file system. 

For older API use

int getBlockCount ()
int getBlockSize ()

StatFs stat = new StatFs(**path**);
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();

Here we need provide correct string value for path variable. You need use string values of path like this: "/mnt/external_sd/" "/mnt/extSdCard/"

You can retreive list of all /mnt/ devices and use one of this values.

File mntDir = new File("/mnt/");
if(mntDir.isDirectory()){ 
String[] dirList = mntDir.list();...}

or something like

var file=new Java.IO.File("storage/");
var listOfStorages=file.ListFiles();

or

String[] externals = System.getenv("SECONDARY_STORAGE").split(":");

Upvotes: 0

user330844
user330844

Reputation: 880

On most phones the total blocks are the number of blocks available to the user (i.e. less system storage etc.). I am yet to find a method that will yield the total amount of storage on the phone.

Upvotes: 0

swap mahan
swap mahan

Reputation: 81

For internal memory Environment.getRootDirectory().getAbsolutePath() is not right approach.

Also on most of devices

Environment.getDataDirectory() works but again not consistent.

For external directory approach is right. To differentiate between internal mnt/sdcard use

Environment.isExternalStorageRemovable().

But i am still wondering about how to get exact internal memory. As with open source inconsistency and lots of versions.

Upvotes: 3

XXX
XXX

Reputation: 9072

/*************************************************************************************************
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;
    }

Upvotes: 3

inazaruk
inazaruk

Reputation: 74780

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
 }

Upvotes: 34

Related Questions