Android - 如何处理在设备空间不足(内部/外部存储)时保存文件

5

如何在设备内存(内部/外部内存)较低的情况下处理文件保存。我知道如果没有足够的空间,操作系统会抛出IOException异常,但是否有一种优雅地处理这种情况的方法。


1
在编写文件内容之前,先检查可用的空闲空间。 - Lucifer
正如@Lucifer所评论的,在检查可用大小后,如果发现可用大小小于文件大小,则可以使用一些压缩算法来压缩您的文件,但请记住,压缩算法需要时间。 - Abx
请查看以下有关编程的内容:https://dev59.com/tHA75IYBdhLWcg3wP2kg,https://dev59.com/oHA85IYBdhLWcg3wF_m0。 - Lucifer
3个回答

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
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);

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;
    }
}
使用上面的函数如下:
      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?对于一些数据中心应用程序,什么是最佳实践? - Sumit Gulati
在将文件保存到内存之前,您可以检查其大小,这是最佳实践。 - GrIsHu
如何在保存到内存之前检查文件大小 @ GrlsHu - Jai Rajesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接