Why the image is not displayed in the imageItem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    Why the image is not displayed in the imageItem?

    I used the Nokia sample codes for Google Map. It has retrieveStaticI mage method which loads and returns an Image. I them loaded the image to an ImageItem but I get an exception everytime..
    Here are my codes...

    Codes for GoogleMaps class
    <CODE>
    import java.io.ByteArr ayOutputStream;
    import java.io.DataOut putStream;
    import java.io.IOExcep tion;
    import java.io.InputSt ream;
    import java.util.Vecto r;
    import javax.microedit ion.io.Connecto r;
    import javax.microedit ion.io.HttpConn ection;
    import javax.microedit ion.lcdui.Image ;
    import javax.microedit ion.lcdui.Image Item;

    public class GoogleMaps implements Runnable{
    private static final String URL_UNRESERVED =
    "ABCDEFGHIJKLMN OPQRSTUVWXYZ" +
    "abcdefghijklmn opqrstuvwxyz" +
    "0123456789-_.~";
    private static final char[] HEX = "0123456789ABCD EF".toCharArray ();
    Image image;
    int width;
    int height;
    double lat;
    double lng;
    int zoom;
    String format;
    ImageItem imagecmpt;
    // these 2 properties will be used with map scrolling methods. You can remove them if not needed
    public static final int offset = 268435456;
    public static final double radius = offset / Math.PI;

    private String apiKey = null;

    public GoogleMaps(Stri ng key,int width, int height, double lat, double lng, int zoom,
    String format,ImageIte m img) {
    this.width=widt h;
    this.height=hei ght;
    this.lat=lat;
    this.lng=lng;
    this.zoom=zoom;
    this.format=for mat;
    apiKey = key;

    }

    public double[] geocodeAddress( String address) throws Exception {
    byte[] res = loadHttpFile(ge tGeocodeUrl(add ress));
    String[] data = split(new String(res), ',');

    if (!data[0].equals("200")) {
    int errorCode = Integer.parseIn t(data[0]);
    throw new Exception("Goog le Maps Exception: " + getGeocodeError (errorCode));
    }

    return new double[] {
    Double.parseDou ble(data[2]), Double.parseDou ble(data[3])
    };
    }

    public Image retrieveStaticI mage() throws IOException {
    byte[] imageData = loadHttpFile(ge tMapUrl(width, height, lng, lat, zoom, format));
    System.out.prin tln("Address is "+getMapUrl(wid th, height, lng, lat, zoom, format));
    for (int i=0;i<imageData .length;i++){
    System.out.prin tln(imageData[i]);
    }
    return Image.createIma ge(imageData, 0, imageData.lengt h);
    }

    private static String getGeocodeError (int errorCode) {
    switch (errorCode) {
    case 400:
    return "Bad request";
    case 500:
    return "Server error";
    case 601:
    return "Missing query";
    case 602:
    return "Unknown address";
    case 603:
    return "Unavailabl e address";
    case 604:
    return "Unknown directions";
    case 610:
    return "Bad API key";
    case 620:
    return "Too many queries";
    default:
    return "Generic error";
    }
    }

    private String getGeocodeUrl(S tring address) {
    return "http://maps.google.com/maps/geo?q=" + urlEncode(addre ss) + "&output=csv&ke y="
    + apiKey;
    }

    private String getMapUrl(int width, int height, double lng, double lat, int zoom, String format) {
    return "http://maps.google.com/staticmap?cente r=" + lat + "," + lng + "&format="
    + format + "&zoom=" + zoom + "&size=" + width + "x" + height + "&key=" + apiKey;
    }

    private static String urlEncode(Strin g str) {
    StringBuffer buf = new StringBuffer();
    byte[] bytes = null;
    try {
    ByteArrayOutput Stream bos = new ByteArrayOutput Stream();
    DataOutputStrea m dos = new DataOutputStrea m(bos);
    dos.writeUTF(st r);
    bytes = bos.toByteArray ();
    } catch (IOException e) {
    // ignore
    }
    for (int i = 2; i < bytes.length; i++) {
    byte b = bytes[i];
    if (URL_UNRESERVED .indexOf(b) >= 0) {
    buf.append((cha r) b);
    } else {
    buf.append('%') .append(HEX[(b >> 4) & 0x0f]).append(HEX[b & 0x0f]);
    }
    }
    return buf.toString();
    }

    private static byte[] loadHttpFile(St ring url) throws IOException {
    byte[] byteBuffer;

    HttpConnection hc = (HttpConnection ) Connector.open( url);
    try {
    hc.setRequestMe thod(HttpConnec tion.GET);
    InputStream is = hc.openInputStr eam();
    try {
    int len = (int) hc.getLength();
    if (len > 0) {
    byteBuffer = new byte[len];
    int done = 0;
    while (done < len) {
    done += is.read(byteBuf fer, done, len - done);
    }
    } else {
    ByteArrayOutput Stream bos = new ByteArrayOutput Stream();
    byte[] buffer = new byte[512];
    int count;
    while ( (count = is.read(buffer) ) >= 0 ) {
    bos.write(buffe r, 0, count);
    }
    byteBuffer = bos.toByteArray ();
    }
    } finally {
    is.close();
    }
    } finally {
    hc.close();
    }

    return byteBuffer;
    }

    private static String[] split(String s, int chr) {
    Vector res = new Vector();

    int curr;
    int prev = 0;

    while ( (curr = s.indexOf(chr, prev)) >= 0 ) {
    res.addElement( s.substring(pre v, curr));
    prev = curr + 1;
    }
    res.addElement( s.substring(pre v));

    String[] splitted = new String[res.size()];
    res.copyInto(sp litted);

    return splitted;
    }

    public void run() {
    try{
    //throw new UnsupportedOper ationException( "Not supported yet.");
    imagecmpt.setIm age(retrieveSta ticImage());
    }
    catch(Exception e){
    System.out.prin tln("Error ......."+e);
    }
    }
    }

    </CODE>

    codes for midlet
    <CODE>
    public void commandAction(C ommand command, Displayable displayable) {
    // write pre-action user code here
    if (displayable == form) {
    if (command == exitCommand) {
    // write pre-action user code here
    exitMIDlet();
    // write post-action user code here
    } else if (command == okCommand) {
    // write pre-action user code here

    // write post-action user code here

    GoogleMaps cMz=new GoogleMaps("ABQ IAAAADEQoVqbqS5 pT4ahHSLALyBT8P MUw5z7_OLJoE1lh 2VQyfb-WOxTwS9t9mrSq_f lhdPeVGOQrxXuCF Q",10, 10, 10, 10, 1, "roadmap", imageItem);

    Thread th=new Thread(cMz);
    th.run();
    Display display=getDisp lay ();


    }
    }
    // write post-action user code here
    }

    </CODE>

    My output:::

    Starting emulator in execution mode
    Running with storage root DefaultColorPho ne
    Running with locale: English_United States.1252
    Running in the identified_thir d_party security domain
    Warning: To avoid potential deadlock, operations that may block, such as
    networking, should be performed in a different thread than the
    commandAction() handler.

    I have used an external thread but yet it keeps telling me to use another thread..
    How can I sort this out?
Working...