Getting all the total and available space on Android

余生颓废 提交于 2019-12-28 02:07:31

问题


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


回答1:


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
 }



回答2:


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



回答3:


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.




回答4:


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.




回答5:


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(":");


来源:https://stackoverflow.com/questions/4799643/getting-all-the-total-and-available-space-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!