Getting all the total and available space on Android
To my knowled开发者_开发百科ge, 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
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
}
/*************************************************************************************************
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;
}
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.
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.
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(":");
精彩评论