Android - 如何在低设备内存(内部/外部内存)上处理保存文件

时间:2013-04-18 06:37:08

标签: android memory

我们如何在低设备内存(内部/外部存储器)上处理文件保存。我知道如果没有足够的空间,操作系统会抛出IOException,但有没有办法优雅地处理它。

3 个答案:

答案 0 :(得分:1)

首先找到内部和外部存储空间的可用空间..然后检查后您的文件大小小于可用内存(根据您的要求选择内部或外部)然后存储消息警报显示... 如果你需要格式大小,那么使用formatSize ....的函数

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

由于

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您可以尝试使用以下功能检查内存是否可用。

/**
 * This function find outs the free space for the given path.
 * 
 * @return Bytes. Number of free space in bytes.
 */
public static long getFreeSpace()
{
    try
    {
        if (Environment.getExternalStorageDirectory() != null
                && Environment.getExternalStorageDirectory().getPath() != null)
        {
            StatFs m_stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
            long m_blockSize = m_stat.getBlockSize();
            long m_availableBlocks = m_stat.getAvailableBlocks();
            return (m_availableBlocks * m_blockSize);
        }
        else
        {
            return 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return 0;
    }
}

将上述功能用作belo:

      int m_fileSizeInKB = m_fileSize / 1024;
    if (m_fileSize >= getFreeSpace())
   {
       //Show the message to user that there is not enough space available in device.
      }
      else
      {
         //your business logic here.
      }