×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    INDIA
    Posts
    13

    Unhappy out of memory error in image manipulation

    hello friends,

    I am developing an app tht sends image to server with help of http multipart request.
    Nw, I want to convert the image in byte array.
    Code:
        public static byte[] getByteArray(Image image)
        {
            int raw[] = new int[image.getWidth() * image.getHeight()];
            image.getRGB(raw, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
            byte rawByte[] = new byte[image.getWidth() * image.getHeight() * 4];
    
            int n = 0;
            for(int i = 0; i < raw.length; i++)
            {
                int ARGB = raw[i];
                int a = (ARGB & 0xff000000) >> 24;
                int r = (ARGB & 0xff0000) >> 16;
                int g = (ARGB & 0xff00) >> 8;
                int b = ARGB & 0xff;
                rawByte[n] = (byte)b;
                rawByte[n + 1] = (byte)g;
                rawByte[n + 2] = (byte)r;
                rawByte[n + 3] = (byte)a;
                n += 4;
            }
    
            raw = null;
            return rawByte;
        }
    when the 5th line "byte rawByte[] = new byte[image.getWidth() * image.getHeight() * 4];" got executed, its raising outofmemory exception....

    wt should i do ??
    Do I have any othe way to convert an image to byte array ??

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: out of memory error in image manipulation

    Hi,
    Can you make sure that how many times this methods executes?
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: out of memory error in image manipulation

    How big is the image (width and height)?

    Why do you want to convert it into an array of pixels?

    Cheers,
    Graham.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    INDIA
    Posts
    13

    Smile Re: out of memory error in image manipulation

    thnx for the replay Raj, Grahamhughes

    by doing some R&D and paperwork, finally i have removed the posted code with the below new one,

    Code:
        public byte[] getImageBytes(String path)
        {
            byte[] imgData = new byte[0];
    
            try
            {
                FileConnection fc = (FileConnection) Connector.open(path, Connector.READ);
                InputStream stream = fc.openInputStream();
                long File_Size = fc.fileSize();
                final int CHUNK = 1;
    
                int length = 0;
                
                while(length < File_Size)
                {
                    byte[] data = new byte[CHUNK];
                    int readAmount = stream.read(data, 0, CHUNK);
                    byte[] newImageData = new byte[imgData.length + CHUNK];
                    System.arraycopy(imgData, 0, newImageData, 0, length);
                    System.arraycopy(data, 0, newImageData, length, readAmount);
                    imgData = newImageData;
                    length += readAmount;
                }
    
            }catch(Exception e) {}
            return imgData;
        }
    I want to send the image to an http server. For that I need to convert it into byte array.

    nw i can read my image file data chunk by chunk.

    by the way thnx lot 4 the replay. thnx 1s again...
    Last edited by vdx; 2009-04-13 at 12:25.
    Good judgment comes from experience, and experience comes from bad judgment.

  5. #5
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: out of memory error in image manipulation

    Hi,
    Since this is a very important thread/subject asked by the developers,so I would appreciate you if can share the solution.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. Image size on 6280
    By ariekk in forum Mobile Java General
    Replies: 0
    Last Post: 2008-09-15, 06:42
  2. Using Camera API requires too much memory
    By tote_b5 in forum Symbian Media (Closed)
    Replies: 18
    Last Post: 2008-07-31, 16:21
  3. Excessive midlet memory consumption
    By paulip in forum Mobile Java General
    Replies: 1
    Last Post: 2007-10-09, 19:24
  4. --- ???save image problem??? ---
    By ferenn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2007-10-01, 14:33
  5. show image on the form from memory
    By aaapigfly in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2007-08-27, 20:15

Posting Permissions

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