×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    How to tackle OutOfMemoryError..??

    When I try to load images from phone memmory I get Out Of Memory errorjava/lang/OutOfMemoryError or nativedecodeImage . Some images in phone memory are of 12kd and while some others are around 589kb or 600kb. The small sized images are fetched to a list but when it comes to larger sized images it thrown the OOM error..??

    This is my code
    Code:
    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);
     }
    The point where I get error is at
    Code:
    image = Image.createImage(imageData, 0, length);
    Does anyone have any idea about this. Am stuck with this thing for few days. Am working on S40 devices Nokia 311. Netbeans MIDP 2.0

  2. #2
    Regular Contributor
    Join Date
    Dec 2012
    Location
    India
    Posts
    271

    Re: How to tackle OutOfMemoryError..??

    The problem lies with the amount of Heap Memory Available.S40 devices have maximum of 2-3 MB of Heap Memory.The JPEG Image ,when decoded, takes up quite a bit of memory.Smaller size images will fit into the 2 MB Heap space,when decoded,whereas larger ones will cause OOM.

    Check this :

    http://developer.nokia.com/Community...a-311-asha-s40

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    7

    Re: How to tackle OutOfMemoryError..??

    Do you have any code to resize the image... or have any idea to overcome this...?

  4. #4
    Regular Contributor
    Join Date
    Dec 2012
    Location
    India
    Posts
    271

    Re: How to tackle OutOfMemoryError..??

    It is not possible to exactly resize since the image must be fully loaded into memory before any resize operation can be done.Your best option would be to extract the thumbnail image as mentioned here http://atif-razzaq.blogspot.no/2009/...g-exif.html.Do note that the resolution will be poor.

  5. #5
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,557

    Re: How to tackle OutOfMemoryError..??

    As far as I know, a JPEG image file can be resized without loading it first fully into memory. How to do it in J2ME code is a different matter, but I'm sure that suitable algorithms/logic are discoverable in, e.g., ImageMagic sources:

    http://www.imagemagick.org/script/index.php

    Similar Java code might also already be available, but I'm not aware of such.

Similar Threads

  1. OutOfMemoryError
    By ArenaInteractive in forum Mobile Java Networking & Messaging & Security
    Replies: 3
    Last Post: 2011-08-16, 17:23
  2. Replies: 9
    Last Post: 2011-02-08, 12:56
  3. OutOfMemoryError
    By prithwi in forum Audio
    Replies: 1
    Last Post: 2009-07-09, 09:28
  4. OutOfMemoryError!!
    By jules_ in forum Mobile Java General
    Replies: 13
    Last Post: 2009-06-04, 15:43
  5. OutOfMemoryError
    By earamsey in forum Mobile Java General
    Replies: 1
    Last Post: 2007-08-30, 19:57

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×