【问题标题】:low memory warning when saving files on blackberry在黑莓上保存文件时内存不足警告
【发布时间】:2013-02-18 09:48:16
【问题描述】:

我想在 Sdcard 上保存许多文件。当我将这些文件保存在文件夹中时,我收到内存不足的警告

设备内存太低-请关闭以下项目

应用程序崩溃了。 我可以手动放置文件夹并且我没有遇到内存问题但在应用程序中它显示它即使SD卡上有可用空间的问题。 这是我用来保存文件的方法。

public static void saveWebContentCache(String save_name, String url) {

    FileConnection fconn = null;
    OutputStream outputStream = null;
    try {
        fconn = (FileConnection) Connector.open(
                NetWorkConfig.webfolder + save_name,
                Connector.READ_WRITE);
        if (!fconn.exists()) {
            fconn.create();
            outputStream = fconn.openOutputStream();

            outputStream.write(getByte(url));

        }
    } catch (IOException e) {
        Status.show("ko !");
    } finally {// Close the connections
        try {
            if (outputStream != null) {
                outputStream.close();
                outputStream = null;
            }

        } catch (Exception e) {
        }
        try {
            if (fconn != null) {
                fconn.close();
                fconn = null;
            }

        } catch (Exception e) {
        }
    }
}

【问题讨论】:

  • 应用失败的文件大小是多少?
  • 我没有静态文件。每个文件都有一个大小。但是文件夹的大小大约是1.8 Mo
  • 我猜“Mo”是 MByte?

标签: memory blackberry java-me


【解决方案1】:

至于我,这条线看起来很可疑:

outputStream.write(getByte(url));

这是因为这种实现意味着您必须在 RAM 中创建/保存整个字节数组,然后再将其写入文件的 OutputStream

相反,您可以结合从 http 连接的 InputSteam 读取小块并将这些块写入文件的 OutputStream。像这样的:

void copyData(InputStream source, OutputStream destination) throws IOException {
    byte[] buf = new byte[1024];
    int len;
    while ((len = source.read(buf)) > 0) {
        destination.write(buf, 0, len);
    }
    destination.flush();
}

【讨论】:

  • getByte(url) 是我从 url 获取的数据字节 [] 以将其写入文件
  • 我没有将数据从文件复制到另一个文件。我从 url 获取数据,然后将其保存到文件中
  • 是的,我明白这一点,但我 100% 确定您的 getByte(url) 使用 InputStream 从 http 连接获取 byte[]
  • 是的,当我使用你的方法时,我得到了运行时异常。这是我如何获取它的输入流以及我在示例 HttpConnection conn = getHttpConnection(url); 中所做的输出conn.setRequestProperty("Cookie", cookies); InputStream 输入 = conn.openInputStream();
  • 查看net.rim.device.api.system.Memory 了解详情。但是我相信如果 RAM 短缺是问题所在,那么您应该得到一个 OutOfMemoryException(事实并非如此)。
猜你喜欢
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
相关资源
最近更新 更多