【问题标题】:How to determine if internal storage is full?如何判断内部存储是否已满?
【发布时间】:2013-05-23 09:23:56
【问题描述】:

我正在制作一个 android 应用程序,它在内部存储中保存一个 2MB .txt 文件。如何知道内部存储是否已满,从而将文件保存到外部存储?

【问题讨论】:

  • 如果空间不足则检查异常

标签: android storage


【解决方案1】:

经过快速搜索,我找到了以下方法。您可以从StatFs 类中获得它。此类用于检索有关文件系统空间的整体信息。

public int FreeMemory()
{
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    int Free  = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
    return Free;
}

也可以通过以下方式找到

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);

您可以从herehere too 获得更多详细信息。

我希望这会对你有所帮助。谢谢。

【讨论】:

    【解决方案2】:

    请检查下面的代码(我已经在这个网站上复制了它,但我不记得它到底在哪里)

    public static boolean externalMemoryAvailable() {
        return android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
    }
    
    public static String getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);
    }
    
    public static String getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return formatSize(totalBlocks * blockSize);
    }
    
    public static String getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return formatSize(availableBlocks * blockSize);
        } else {
            return ERROR;
        }
    }
    
    public static String getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return formatSize(totalBlocks * blockSize);
        } else {
            return ERROR;
        }
    }
    
    public static String formatSize(long size) {
        String suffix = null;
    
        if (size >= 1024) {
            suffix = "KB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MB";
                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();
    }
    

    希望能帮助你。

    【讨论】:

      【解决方案3】:

      你试过float freeSpace = DeviceMemory.getInternalFreeSpace();吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-11
        • 2010-12-20
        • 2012-07-11
        • 1970-01-01
        • 2010-12-26
        • 2017-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多