Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

environment - Getting all the total and available space on Android

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...