Android 查看可用存储内存大小

下面为查看可用存储内存大小的示例,用于查看和内部和外部存储器的总存储。

 

Java代码 复制代码
  1. import java.io.File;      
  2.      
  3. import android.os.Environment;      
  4. import android.os.StatFs;      
  5.      
  6. public class MemoryStatus {      
  7.      
  8.     static final int ERROR = -1;      
  9.           
  10.     static public boolean externalMemoryAvailable() {      
  11.         return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);      
  12.     }      
  13.           
  14.     static public long getAvailableInternalMemorySize() {      
  15.         File path = Environment.getDataDirectory();      
  16.         StatFs stat = new StatFs(path.getPath());      
  17.         long blockSize = stat.getBlockSize();      
  18.         long availableBlocks = stat.getAvailableBlocks();      
  19.         return availableBlocks * blockSize;      
  20.     }      
  21.           
  22.     static public long getTotalInternalMemorySize() {      
  23.         File path = Environment.getDataDirectory();      
  24.         StatFs stat = new StatFs(path.getPath());      
  25.         long blockSize = stat.getBlockSize();      
  26.         long totalBlocks = stat.getBlockCount();      
  27.         return totalBlocks * blockSize;      
  28.     }      
  29.           
  30.     static public long getAvailableExternalMemorySize() {      
  31.         if(externalMemoryAvailable()) {      
  32.             File path = Environment.getExternalStorageDirectory();      
  33.             StatFs stat = new StatFs(path.getPath());      
  34.             long blockSize = stat.getBlockSize();      
  35.             long availableBlocks = stat.getAvailableBlocks();      
  36.             return availableBlocks * blockSize;      
  37.         } else {      
  38.             return ERROR;      
  39.         }      
  40.     }      
  41.           
  42.     static public long getTotalExternalMemorySize() {      
  43.         if(externalMemoryAvailable()) {      
  44.             File path = Environment.getExternalStorageDirectory();      
  45.             StatFs stat = new StatFs(path.getPath());      
  46.             long blockSize = stat.getBlockSize();      
  47.             long totalBlocks = stat.getBlockCount();      
  48.             return totalBlocks * blockSize;      
  49.         } else {      
  50.             return ERROR;      
  51.         }      
  52.     }      
  53.           
  54.     static public String formatSize(long size) {      
  55.         String suffix = null;      
  56.           
  57.         if (size >= 1024) {      
  58.             suffix = "KiB";      
  59.             size /= 1024;      
  60.             if (size >= 1024) {      
  61.                 suffix = "MiB";      
  62.                 size /= 1024;      
  63.             }      
  64.         }      
  65.           
  66.         StringBuilder resultBuffer = new StringBuilder(Long.toString(size));      
  67.           
  68.         int commaOffset = resultBuffer.length() - 3;      
  69.         while (commaOffset > 0) {      
  70.             resultBuffer.insert(commaOffset, ',');      
  71.             commaOffset -= 3;      
  72.         }      
  73.           
  74.         if (suffix != null)      
  75.             resultBuffer.append(suffix);      
  76.         return resultBuffer.toString();      
  77.     }      
  78. }    
import java.io.File;   
  
import android.os.Environment;   
import android.os.StatFs;   
  
public class MemoryStatus {   
  
    static final int ERROR = -1;   
       
    static public boolean externalMemoryAvailable() {   
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);   
    }   
       
    static public long getAvailableInternalMemorySize() {   
        File path = Environment.getDataDirectory();   
        StatFs stat = new StatFs(path.getPath());   
        long blockSize = stat.getBlockSize();   
        long availableBlocks = stat.getAvailableBlocks();   
        return availableBlocks * blockSize;   
    }   
       
    static public long getTotalInternalMemorySize() {   
        File path = Environment.getDataDirectory();   
        StatFs stat = new StatFs(path.getPath());   
        long blockSize = stat.getBlockSize();   
        long totalBlocks = stat.getBlockCount();   
        return totalBlocks * blockSize;   
    }   
       
    static public long getAvailableExternalMemorySize() {   
        if(externalMemoryAvailable()) {   
            File path = Environment.getExternalStorageDirectory();   
            StatFs stat = new StatFs(path.getPath());   
            long blockSize = stat.getBlockSize();   
            long availableBlocks = stat.getAvailableBlocks();   
            return availableBlocks * blockSize;   
        } else {   
            return ERROR;   
        }   
    }   
       
    static public long getTotalExternalMemorySize() {   
        if(externalMemoryAvailable()) {   
            File path = Environment.getExternalStorageDirectory();   
            StatFs stat = new StatFs(path.getPath());   
            long blockSize = stat.getBlockSize();   
            long totalBlocks = stat.getBlockCount();   
            return totalBlocks * blockSize;   
        } else {   
            return ERROR;   
        }   
    }   
       
    static public String formatSize(long size) {   
        String suffix = null;   
       
        if (size >= 1024) {   
            suffix = "KiB";   
            size /= 1024;   
            if (size >= 1024) {   
                suffix = "MiB";   
                size /= 1024;   
            }   
        }   
       
        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));   
       
        int commaOffset = resultBuffer.length() - 3;   
        while (commaOffset > 0) {   
            resultBuffer.insert(commaOffset, ',');   
            commaOffset -= 3;   
        }   
       
        if (suffix != null)   
            resultBuffer.append(suffix);   
        return resultBuffer.toString();   
    }   
}  

 

 

 来个更简单的

 

Java代码 复制代码
  1.     private void update() {   
  2.     File path = Environment.getExternalStorageDirectory();   
  3.     StatFs stat = new StatFs(path.getPath());   
  4.     long blockSize = stat.getBlockSize();   
  5.     long totalBlocks = stat.getBlockCount();   
  6.     long availableBlocks = stat.getAvailableBlocks();   
  7.     mTotalSize.setText(formatSize(totalBlocks * blockSize));   
  8.     mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));   
  9.     mAvailableSize.setText(formatSize(availableBlocks * blockSize));   
  10.   
  11.   
  12. private String formatSize(long size) {   
  13.     return Formatter.formatFileSize(this, size);   
  14. }  

你可能感兴趣的:(android,OS)