【问题标题】:Android - how to handle saving file on low device memory(Internal/External memory)Android - 如何在低设备内存(内部/外部内存)上处理保存文件
【发布时间】:2013-04-18 06:37:08
【问题描述】:

我们如何处理低设备内存(内部/外部内存)上的文件保存。我知道如果没有足够的空间,操作系统会抛出 IOException,但有什么办法可以优雅地处理这个问题。

【问题讨论】:

标签: android memory


【解决方案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】:

    首先找到内部和外部内存空间的可用空间..然后检查您的文件大小小于可用内存(根据您的要求选择内部或外部)然后存储否则消息警报显示... 如果您需要格式大小,请使用 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();
        }
    

    谢谢

    【讨论】:

      【解决方案3】:

      你试试下面的函数来检查内存的可用性。

      /**
       * 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.
            }
      

      【讨论】:

      • 大家好,非常感谢您的回复。如果在保存文件之前不知道文件大小,我还有一个查询。那么我们如何处理内存不足的情况。是否需要通过在保存文件或捕获 IOException 之前检查文件空间来处理相同的问题。某些以数据为中心的应用程序的最佳做法是什么?
      • 您可以在将文件保存到内存之前检查文件大小,这是最佳做法。
      • 如何在保存到内存之前检查文件大小@GrlsHu,
      猜你喜欢
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2022-08-10
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多