2011-02-03 4 views
0

TimerTask 및 ImageLoader 클래스를 사용하여 n 이미지를 이미지 항목에로드합니다.J2ME에서 이미지를로드하려면 어떻게해야합니까?

public class Imageloader implements Runnable{ 

    private ImageItem item=null; 
    private String url=null; 

    /* 
    * initializes the imageItem 
    */ 
    public Imageloader(ImageItem item,String url){ 
      this.item=item; 
      this.url=url; 
    } 


    private Image getImage(String url) throws IOException { 
     item.setLabel(item.getLabel()+12); 
     System.out.println("Test 5"); 
      HttpConnection connection = null; 
      DataInputStream inp = null; 
      int length; 
      byte[] data; 
      try {System.out.println("Test 6"); 
        connection = (HttpConnection) Connector.open(url); 
        item.setLabel(item.getLabel()+13); 
        connection.getResponseMessage(); 
        System.out.println("Test 7"); 
        length = (int) connection.getLength(); 
        item.setLabel(item.getLabel()+14); 
        System.out.println("Length is "+length); 
        System.out.println("Test 8"); 
        data = new byte[length]; 
        inp = new DataInputStream(connection.openInputStream()); 
        item.setLabel(item.getLabel()+15); 
        System.out.println("Test 9"); 
        inp.readFully(data); 
        item.setLabel(item.getLabel()+16); 
        System.out.println("Test 10"); 
        return Image.createImage(data, 0, data.length); 
      } 
      finally { 
       if (connection != null) connection.close(); 
       if (inp != null)inp.close(); 

      } 
    } 


    public void run() { 
     System.out.println("Test 1"); 
     Image image=null; 
     try{ 
       if (url!=null){ 
        System.out.println("Test 2"); 
         image=getImage(url); 
         System.out.println("Test 3"); 
         item.setImage(image); 
         item.setLabel(item.getLabel()+17); 
         System.out.println("Test 4"); 
       } 
       else{ 
        item.setAltText("Map address specified is incorrect"); 
       } 
     } 
     catch(IOException e){ 
       item.setAltText("Map cannot be loaded now"); 
     } 

    } 

} 

public class MapTimer extends TimerTask{ 

    DatagramConnection connection=null; 
    String message=null; 
    Imageloader imageretriever; 
    ImageItem item=null; 

    MapTimer (DatagramConnection connection,String message,ImageItem Img_map){ 
     this.connection=connection; 
     this.message=message; 
     this.item=Img_map; 
     item.setLabel(item.getLabel()+1); 
     System.out.println("Map is initizlized..."); 
    } 


    public void run() { 
     System.out.println("Map starting..."); 
     item.setLabel(item.getLabel()+2); 
     String serverquery=null; 
     try { 
      item.setLabel(item.getLabel()+3); 
      sendMessage(message); 
      item.setLabel(item.getLabel()+4); 
      //serverquery=receiveMessage() ; 
      item.setLabel(item.getLabel()+5); 
      //item.setLabel(" Loading...."); 
      //formatmessage(serverquery); 
       String url="http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap"+ 
"&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74."+ 
"&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false"; 
      Imageloader Im=new Imageloader(item,url); 
      item.setLabel(item.getLabel()+6); 
      (new Thread(Im)).start(); 
      item.setLabel(item.getLabel()+7); 
      System.out.println("server map query is::: "+serverquery); 
     } catch (IOException ex) { 
      System.out.println("Error2"+ex); 
     } 
      catch(Exception e){ 
      System.out.println("Error3"+e); 
     } 
    } 


    /* 
    * Sends a message via UDP to server 
    */ 
    private void sendMessage(String message) throws IOException{ 
     Datagram packet_send=null; 
     if (connection != null){ 
      byte[] packetsenddata=message.getBytes(); 
      packet_send = connection.newDatagram(packetsenddata,packetsenddata.length); 
      connection.send(packet_send); 
      packet_send = null; 
     } 
    } 
} 

이것은 타이머를 설정하는 방법입니다.

MapTimer maptimer=new MapTimer (connection,mapquery ,Img_map); 
        Timer timer=new Timer(); 
        timer.schedule(maptimer, 5000, 100000); 

IT는 enulator과 잘 일하고있어하지만 난 내 마피아에 배포로, 이미지는 이미지 레이블이 다소 내 타이머가 잘 실행되고 있음을 의미한다 Stopping 234567234567 같다 .. 를로드하지 않습니다. ImageLoader 클래스에 들어 가지 않습니다 ...이 이미지를로드하려면 어떻게해야합니까?

답변

2

추가 디버깅 없이는 말하기 어렵습니다. 기기에서 미드 레트를 디버그하려면 Micrologger + 웹 서버를 사용하는 것이 좋습니다.

코드를 보면이 줄이 의심스러운 것 같습니다. length = (int) connection.getLength();. Nokia의 IO 라이브러리 구현에 실패 할 수 있습니까?

관련 문제