【问题标题】:Out Of memory Error android内存不足错误android
【发布时间】:2012-03-07 17:10:47
【问题描述】:
        byte[] pdfBinary ;
        File file1 = new File("/mnt/sdcard/","metaphysics.pdf");
        Log.d(TAG, "got a file reference");
        Uri uri1 = Uri.fromFile(file1);

        pdffilename = file1.getName();
        Log.d(TAG, "got File name");
        pdfBinary = readUriContent(uri1);
        Log.d(TAG, "read from uri");

我试图获取 PDF 文件的缩略图。为此,我必须将我的 PDF 文件以二进制形式存储在 RAM 中。对于小文件,它很好,但对于像 20MB 这样的大文件,它会显示 java.lang.OutofMemoryError。任何建议我该怎么做。

提前致谢。

【问题讨论】:

  • 这个问题问了很多次做一些研发然后在这里发帖......

标签: android out-of-memory


【解决方案1】:

看看this answer

想法:将缓冲区映射到文件通道并仅读取第一页。这应该会减少内存消耗。

PDF 渲染器也已移植到安卓:Andpdf

希望对你有所帮助。

【讨论】:

  • 感谢您的回复。我知道 Andpdf 并且我正在使用他们的代码。但问题是内存不足异常。像 500KB 这样的小文件效果很好
  • ' private byte[] readUriContent(Uri uri) { byte[] result = null;尝试 { InputStream is = getContentResolver().openInputStream(uri); int size = is.available();结果=新字节[大小];整数位置 = 0; int cnt = is.read(result, pos, size-pos);而 (cnt > 0) { pos += cnt; cnt = is.read(result, pos, size-pos); } is.close(); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } 返回结果; } '
  • 请用我上面的链接中发布的 Java NIO 测试读取文件。这应该比将洞文件读入字节缓冲区消耗更少的内存,因为它只将文件系统缓冲区映射到 Java。
猜你喜欢
  • 2013-01-17
  • 2013-11-01
  • 1970-01-01
  • 2014-12-30
  • 2013-01-17
  • 1970-01-01
  • 2013-05-13
  • 2011-12-08
相关资源
最近更新 更多