convert image into byte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbs
    New Member
    • Dec 2007
    • 1

    convert image into byte

    hi all of u ,

    in my project i want to transfer an image from one moblie to another mobile using bluetooth. i have written a code but giving null pointer exception at
    int length = in.read(buff);

    my code is :

    [code=java]
    public void imageTransfer()
    {
    System.err.prin tln(" From image transfer ()");
    String imgName = "/Sunset.png";
    imgData = getImageData(im gName);
    sendImageData(i mgData);

    }

    public byte[] getImageData(St ring imgName)
    {


    in = getClass().getR esourceAsStream (imgName);

    buff = new byte[1024];
    baos = new ByteArrayOutput Stream(1024);

    try
    { System.err.prin tln(" From getImageData() try block : ");
    int length = in.read(buff);
    }
    catch(Exception e)
    {
    e.printStackTra ce();
    }



    try
    {

    while(true)
    {

    if(length == -1)
    {
    break;
    }

    baos.write(buff ,0,length);
    }
    }
    catch(Exception e)
    {
    e.printStackTra ce();
    }
    return baos.toByteArra y();

    }

    public void sendImageData(b yte[] imgData)
    {

    System.err.prin tln(" From sendImageData() ");
    if(imgData == null)
    {
    return ;
    }

    OutputStream out = null;

    try
    {
    System.err.prin tln(" From getImageData() try ");
    out = i.openOutputStr eam();
    out.write(imgDa ta);
    out.flush();

    }
    catch(IOExcepti on e)
    {
    System.out.prin tln(""+e.getMes sage());
    }

    if (out != null)
    {
    try
    {
    out.close();
    }
    catch (IOException e)
    {
    }
    }
    }
    [/code]
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    Moved to the JAVA Forums where the resident experts can better assist you, and please use code tags.

    **Moved from Programming Challenges

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by sbs
      hi all of u ,

      in my project i want to transfer an image from one moblie to another mobile using bluetooth. i have written a code but giving null pointer exception at
      int length = in.read(buff);
      No need to look at the code any further: if you receive that exception on that
      line of your code than 'in' equals null; no doubt about it.

      kind regards,

      Jos

      Comment

      Working...