This discussion is archived
1 Reply Latest reply: Mar 9, 2007 5:52 AM by 807597 RSS

Reading and Writing Image through Socket

807597 Newbie
Currently Being Moderated
hi
i'm doing a mobileapplication and i send the Image from the server using a DataOutputStream. I'm converting the Image to a byteArray. The Image in .png format. But i need to read it from a J2ME application and when i read the byteArray and convert it to a Image it gives a illegalArgumentException coz it says that cannot decode the Image.

Can anyone Help

Rushi
  • 1. Re: Reading and Writing Image through Socket
    807597 Newbie
    Currently Being Moderated
    some png files are not standard png files thats why u may get the error but u should try like this:
    Server side=>

    OutputStream os=c.getOutputStream();
    int numBytesRead = 0;
    while ((numBytesRead = input.read(buf)) != -1) {
    os.write(buf, 0, numBytesRead);
    }

    Client side=>

    bStrm=new ByteArrayOutputStream();
    c = 0;
    while ((c=dis.read())!=-1)

    {
    bStrm.write(c);
    }
    imgData = bStrm.toByteArray();
    bStrm.close();
    img=Image.createImage(imgData,0,imgData.length);