×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Mumbai, India
    Posts
    54

    Image through a servlet gives out of memory at the client

    Hi,

    I have a J2me client which needs to request images from a servlet and display them. The code that I am using at the servlet is something like this :

    File f = new File(AppConstants.LOCAL_CONTEXT_PATH+"/"+path);
    BufferedImage img = ImageIO.read(f);

    ServletOutputStream ostr;

    response.setContentType("image/png");

    ostr = response.getOutputStream();

    ImageIO.write(img,"png",ostr);

    ostr.close();

    However, the client residing on the emulator gives me an out of memory exception while trying to read the image.

    Is there a better way to achieve the same, except for direct url hits?

    And the emulator is the standard WTK emulator.

    Awaiting a reply.

    Thanks in advance.
    Last edited by sanket2612; 2008-05-19 at 15:09.

  2. #2
    Nokia Developer Expert
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351

    Re: Image through a servlet gives out of memory at the client

    Hello sanket2612

    Can you post relevant code you are using on the J2ME client to restore the image? and tell us what emulator are you using?

  3. #3
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: Image through a servlet gives out of memory at the client

    Probably the problem is related to your image size, that is too big to be successfully handled by emulator (and probably by real mobile devices too). Trying optimizing/scaling down your image should help..

    Pit

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Mumbai, India
    Posts
    54

    Re: Image through a servlet gives out of memory at the client

    Hi,
    Thanks for your replies.
    My image size ranges from 8 to 15 k.
    Scaling down is feasible for my application.
    The client code goes like this :

    try {
    conn = (HttpConnection)Connector.open(request.getURL(), 3, true);
    // conn = (HttpConnection)Connector.open(formatURL(request.getURL()), 3, true);
    conn.setRequestProperty("User-Agent", userAgent);
    conn.setRequestProperty("Accept-Charset", acceptCharset);
    conn.setRequestProperty("Accept-Language", acceptLanguage);
    conn.setRequestProperty("Connection", "close");
    Hashtable hashtable = request.getRequestProperty();
    if(hashtable != null) {
    String s;
    String s1;
    for(Enumeration enumeration = hashtable.keys(); enumeration.hasMoreElements(); conn.setRequestProperty(s, s1)) {
    s = (String)enumeration.nextElement();
    s1 = (String)hashtable.get(s);
    }
    }
    conn.setRequestMethod(request.getRequestMethod());
    if(request.getRequestMethod().equals("POST")) {
    out = conn.openOutputStream();
    //System.out.println("The data="+new String(request.getBody()));
    out.write(request.getBody());
    out.flush();
    // //System.out.println(request.getURL()+"......The length in the Connector ---------------------------" + request.getByteBody().length);
    out.close();
    }
    c_starttime=System.currentTimeMillis();
    //System.out.println(c_starttime);

    in = conn.openInputStream();

    //System.out.println("After conn.openInputStream");

    int i = (int)conn.getLength();

    //System.out.println("****Length :"+i);

    byte abyte0[] = null;
    if(i == -1) {
    // //System.out.println("******LENGTH OF THE INPUT STREAM NOT KNOWN*******");
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    byte abyte1[] = new byte[512];
    int l = 0;
    try {
    while((l = in.read(abyte1)) != -1 && !timeout)
    {
    if((System.currentTimeMillis()-c_starttime)>ResourceManager.timeout)
    // if((System.currentTimeMillis()-c_starttime)>100)
    {
    ResourceManager.imageTimeout=true;
    timeout=true;
    cleanUp();
    if(conn!=null)
    {
    conn.close();

    }
    // //System.out.println("RETURNING NULL DUE TO TIME OUT AFTER READING"+l);
    return response2;
    }
    bytearrayoutputstream.write(abyte1, 0, l);
    }
    }
    catch(Exception exception2) {
    exception2.printStackTrace();
    }
    abyte0 = bytearrayoutputstream.toByteArray();
    } else {
    abyte0 = new byte[i];
    try {
    int j = 0;
    int k;
    while((k = in.read(abyte0, j, abyte0.length - j)) > 0 && !timeout) {
    if((System.currentTimeMillis()-c_starttime)>ResourceManager.timeout)
    // if((System.currentTimeMillis()-c_starttime)>100)
    {
    ResourceManager.imageTimeout=true;
    timeout=true;
    }
    j += k;
    // //System.out.println("Length of strem "+abyte0.length+" amt read"+j);
    if(j == abyte0.length)
    break;
    }
    if(j!=abyte0.length)
    {
    // //System.out.println("RESPONSE NULL DUE TO TIME OUT");

    cleanUp();
    if(conn!=null)
    {
    conn.close();
    }
    return response2;
    }
    }
    catch(Exception exception1) {
    exception1.printStackTrace();
    }
    }
    Response response = new Response(conn.getResponseCode(), conn.getType(), abyte0);
    saveResponseHeader(request.getURL(), response);
    response2 = response;
    cleanUp();
    } catch (RuntimeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    //System.out.println("Exception---con--abc"+e.toString());
    }


    It works well when the images are direct url hits, like

    www.xyz.com/123.png

    but not in the eralier case.

    I just want to know whether there is any work around.

    Thanks

Similar Threads

  1. Using Camera API requires too much memory
    By tote_b5 in forum Symbian Media (Closed)
    Replies: 18
    Last Post: 2008-07-31, 16:21
  2. Image attribute in Bluetooth ServiceRecord
    By lampz in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2008-02-18, 20:30
  3. Excessive midlet memory consumption
    By paulip in forum Mobile Java General
    Replies: 1
    Last Post: 2007-10-09, 19:24
  4. 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
  5. Is MIDP2.0 over Symbian OS slow?
    By epolitakis in forum Mobile Java Games
    Replies: 7
    Last Post: 2007-03-16, 09:32

Posting Permissions

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