×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Out of memory error when reading image in phone's memory card

    I'm trying to read images from the phone's memory card and i get the out of memory error:
    java.lang.outofmemoryerror:nativeDecodeImage
    this is my code:
    Code:
    readFile("file:///E:/Images/Photo0008.jpg")
    and the method readFile:
    Code:
    private Image readFile(String path) {
            Image image = null;
            try {
                FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
                if(!fc.exists()) {
                    System.out.println("File doesn't exist!");
                }
                else {
                    int size = (int)fc.fileSize();
                    InputStream is = fc.openInputStream();
                    byte bytes[] = new byte[size];
                    int bytesRead = 0;
                    while (bytesRead < size) {
                        bytesRead += is.read(bytes, bytesRead, size - bytesRead);
                    }
                    image = Image.createImage(bytes, 0, size);
                }
     
            } catch (IOException ioe) {
                System.out.println("IOException: "+ioe.getMessage());
            } catch (IllegalArgumentException iae) {
                System.out.println("IllegalArgumentException: "+iae.getMessage());
            }
            return image;
       }
    What can i do to solve this problem?

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,553

    Re: Out of memory error when reading image in phone's memory card

    How much memory is available for a MIDlet depends on how much RAM the phone has + how much RAM the phone manufacturer decided to allow MIDlets to use.

    How big is the image file? Which phone model are you using? Have you tried to use other phone models? How much memory is free on them (just after boot, with only with your app launched)?

    Otherwise, start optimizing your app's memory use. However, if processing an image will require you to read it in whole (note that the in-memory version is much, much larger, treated as an image and not just data, in bytes than the file), and you still haven't got enough free RAM - hit the limits set by the device manufacturer in terms of the Java environment or the amount of physical memory - then there's not much you can do except notify the user that the phone has too little memory to process the image.

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: Out of memory error when reading image in phone's memory card

    It really matters what device you are using (if it is heap limited or not), and what is that image. When uncompressed, even a 1-megapixel photo consumes 3 megabytes of memory. And nowadays, it is probably some multi-megapixel camera/image in the phone.

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: Out of memory error when reading image in phone's memory card

    Try this on some S60 3rd edition device, check the available free memory just before you call image = Image.createImage(bytes, 0, size);
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  5. #5
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Out of memory error when reading image in phone's memory card

    This article describes in more detail, how the heap is used by Images and you can find the maximum heap size for a specific model under memory functions in the Device specification page.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    14

    Re: Out of memory error when reading image in phone's memory card

    Hi Sundayokopar,

    I am also facing same problem and I am unable to find the solution why it is giving, if you got solution so please tell me.


    Thanks,
    Shailendra

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    14

    java.lang.OutOfMemoryError: nativeDecodeImag

    Hi All,

    I'm trying to read images from the phone's memory and i get the out of memory error:
    java.lang.OutOfMemoryError: nativeDecodeImage please help me........
    this is my code:
    Code:
    readFile("file:///E:/Images/Photo0001.jpg")
    and the method readFile:
    Code:
    private Image readFile(String path) {
    Image image = null;
    try {
    FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
    if(!fc.exists()) {
    System.out.println("File doesn't exist!");
    }
    else {
    int size = (int)fc.fileSize();
    InputStream is = fc.openInputStream();
    byte bytes[] = new byte[size];
    int bytesRead = 0;
    while (bytesRead < size) {
    bytesRead += is.read(bytes, bytesRead, size - bytesRead);
    }
    image = Image.createImage(bytes, 0, size); //Exception Coming here
    }

    } catch (IOException ioe) {
    System.out.println("IOException: "+ioe.getMessage());
    } catch (IllegalArgumentException iae) {
    System.out.println("IllegalArgumentException: "+iae.getMessage());
    }
    return image;
    }
    ///////////////////OR////////////////////
    FileConnection fileConn = (FileConnection)Connector.open(path, Connector.READ);
    // load the image data in memory
    // Read data in CHUNK_SIZE chunks
    InputStream fis = fileConn.openInputStream();
    long overallSize = fileConn.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();
    fileConn.close();
    currentImage = Image.createImage(imageData, 0, length);
    }catch(Exception e){}
    return image;

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: java.lang.OutOfMemoryError: nativeDecodeImag

    Your code is taking more then the double of the memory needed to create this image - try createImage(InputStream stream) directly it might help.
    Check on device with unlimited heap or reduce the size of the image you want to read.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    1

    Re: Out of memory error when reading image in phone's memory card

    I have the same problem, too.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    14

    Re: java.lang.OutOfMemoryError: nativeDecodeImag

    Hi im2amit,

    I have try createImage(InputStream ..) directly but same problem is coming, only I can open some .png file but when I try to open .jpeg file format than it give Exception java.lang.OutOfMemoryError: nativeDecodeImage same problem is coming please help me it's urgent. also I am not getting any solution how solve this things .

    Thanks,
    Shailendra

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: java.lang.OutOfMemoryError: nativeDecodeImag

    You can read about the reasons in http://www.developer.nokia.com/Commu...2-Resize-image, but there is no easy solution.

Similar Threads

  1. Reading messages from memory card
    By pavarang in forum Symbian Networking & Messaging (Closed)
    Replies: 7
    Last Post: 2010-12-20, 21:11
  2. Reading text file stored in phone memory/memory card
    By devdattac in forum Mobile Java General
    Replies: 6
    Last Post: 2008-09-19, 19:30
  3. Help ragarding reading from phone and memory card
    By prats123 in forum Mobile Java General
    Replies: 1
    Last Post: 2008-03-31, 11:00
  4. Replies: 2
    Last Post: 2006-05-02, 07:19

Posting Permissions

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