How to find the amount of free storage (disk space) left on Android?

后端 未结 10 1221
遇见更好的自我
遇见更好的自我 2020-12-02 14:25

I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?

Thanks,

10条回答
  •  佛祖请我去吃肉
    2020-12-02 14:53

    With a little google you might had found the StatFs-class which is:

    [...] a Wrapper for Unix statfs().

    Examples are here and here:

    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();
        }
    }
    

    Source for code above (Wayback Machine)

提交回复
热议问题