×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    Creating image from downloaded file

    Hi all,

    in the code below i download the png file 'testje.png'. This all goes well. But then i want to create an image from the bytearray i downloaded. I get an 'IllegalArgumentException' on:

    m_Img = Image.createImage(img, 0, img.length);

    **************
    package com.notthefly.mobile.Roel_GPRS;

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;

    public class Roel_GPRS extends MIDlet {

    HttpConnection m_Con = null;
    InputStream m_Is = null;
    Display m_Display = null;
    String m_Url = "http://www.solidweb.nl:80/stage/testje.png";
    Image m_Img = null;
    int i = 0;
    int nBYTES = 60000;

    public Roel_GPRS(){
    m_Display = Display.getDisplay(this);
    }

    public void startApp() {
    m_Tb = new TextBox("TestMIDlet", "Opening conn...\n", 1024, 0);
    m_Display.setCurrent(m_Tb);

    try{
    // Open http connection.
    m_Con = (HttpConnection)Connector.open(m_Url);

    /*
    int response = m_Con.getResponseCode();
    System.out.println("RESP: " + response);
    if (response == 200) {
    m_Tb = new TextBox("TestMIDlet", "Connection opened...\n", 1024, 0);
    m_Display.setCurrent(m_Tb);
    }*/

    // Open inputstream
    m_Is = m_Con.openInputStream();

    int length=(int) m_Con.getLength();
    int ch;
    int teller = 0;

    byte image[] = new byte[nBYTES];
    while ((ch = m_Is.read()) != -1) {
    image[count++] = (byte) ch;
    }

    byte img[] = new byte[count];
    for (int i = 0; i < count; i++) {
    img[i] = image[i];
    }

    m_Img = Image.createImage(img, 0, img.length);


    }
    catch(IOException e){
    System.out.println("TEST");
    }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    }

    ********

    I hope for an answer that solves my problem,

    thanks in advance!

    EDIT: i think i'd better placed this topic in the 'java' forum instead of 'networking'.
    Last edited by roelb; 2003-11-19 at 15:52.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    (If you want to copy an array of bytes, don't write a loop... use System.arraycopy())

    Probably, your array of bytes does not contain a valid PNG. This could be because either: the original PNG is not valid (try placing it in your JAR and loading it straight out of there); or the server has not sent you what you think (try printing out some of the byte-values - is it the characters "<?xml", for example?).

    Graham.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    Hi, thanks for the answer.

    The image itself is valid. I tried to load it as a resource and no problems occured. The first bytes in the data i receive is this:
    [exec] BYTE: ?
    [exec] BYTE: P
    [exec] BYTE: N
    [exec] BYTE: G
    [exec] BYTE:
    [exec] BYTE: ¡ú

    Also the number of bytes is excactly the same as the sizes it shows on the server. So it should download the whole file.

    Any suggestions?

    Thanks!

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Is the file on the server the same size as it was before you uploaded it to the server? Those byte values look like it has been treated as text, not binary data. Was it FTP'd to the server in ASCII mode?

    Graham.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    Ohw damn, indeed. I ftp'd it in ascii mode... Not really smart. Thanks man!! This helped me out!!

  6. #6
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Great detective work Graham!

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

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    10
    Hi,

    So I presume your code works for viewing a downloaded .png file right? Do you happen to know how I can download other files (.jar, and .jad for example) and save them on the phone somewhere?? You can see that I'm trying to download midlet/s using a midlet.

    Would really appreciate help here!!

    Chee Wee

Posting Permissions

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