【问题标题】:How to tackle OutOfMemoryError如何解决 OutOfMemoryError
【发布时间】:2013-08-08 06:01:50
【问题描述】:

当我尝试从手机内存中加载图像时,我得到 Out Of Memory errorjava/lang/OutOfMemoryError 或 nativedecodeImage 。手机内存中的一些图像为 12kd,而另一些则为 589kb 或 600kb 左右。小尺寸的图像被提取到一个列表中,但是当涉及到较大尺寸的图像时,它会抛出 OOM 错误..??

这是我的代码

FileConnection finalConnection;
 try
    {     
        fc.close();
        finalConnection = (FileConnection) Connector.open(path, Connector.READ_WRITE);
        if(finalConnection.exists())
        {
            InputStream fis = finalConnection.openInputStream();
            long overallSize = finalConnection.fileSize();
            int length = 0;
            byte[] imageData = new byte[0];
            while (length < overallSize)
            {
                byte[] data = new byte[CHUNK_SIZE];
                int readAmount = fis.read(data, 0, CHUNK_SIZE);
                byte[] newImageData = new byte[imageData.length + CHUNK_SIZE];
                System.arraycopy(imageData, 0, newImageData, 0, length);
                System.arraycopy(data, 0, newImageData, length, readAmount);
                imageData = newImageData;
                length += readAmount;
            }
            fis.close();
            finalConnection.close();
            System.out.println("LENGTH IS " + length);
            if (length > 0)
            {                    
                image = Image.createImage(imageData, 0, length);

            }
        }
        else
        {
            System.out.println("NO PATH FOR IMAGE");
        }
    }
 catch (Exception e)
    {
        System.out.println("Image.createImage(imageData, 0, length) " +e.toString());
    }
 catch(Error e)
 {
     System.out.println("Image.createImage " + e);
 }        

我得到错误的地方是

image = Image.createImage(imageData, 0, length);

有没有人对此有任何想法。我被这件事困住了几天。我正在开发 S40 设备 Nokia 311。Netbeans MIDP 2.0

【问题讨论】:

    标签: image java-me netbeans-7 file-connection


    【解决方案1】:

    线索在异常中...您打开的图像太大而无法保存在内存中。请记住,图像的压缩大小并不一定表明它是否可以容纳,它实际上与像素数有关。

    如果它太大,您无能为力...如果您想在屏幕上显示图像,您需要先对其进行一些处理以缩小它,然后才能将其加载到内存中。

    【讨论】:

      【解决方案2】:

      大多数仅支持 JavaME 的设备在这方面受到限制,您无法加载该尺寸的图像。

      但也许你不需要。

      许多相机会生成所拍摄照片的缩略图版本 - 并将此缩略图包含在 EXIF 数据中。因此,当您需要在手机上显示照片列表时,您无需加载整张照片。您只需从文件中的 exif 数据加载缩略图数据。

      不过,我当时只是对此进行了短暂的实验。在网上找到了一些应该可以解决问题的示例源代码,但很少奏效。这可能部分是因为我测试的手机没有在 exif 数据中保存缩略图。也许 Asha 311 可以?

      【讨论】:

        猜你喜欢
        • 2012-02-12
        • 1970-01-01
        • 2020-04-11
        • 2015-11-17
        • 2015-07-29
        • 1970-01-01
        • 2021-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多