java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4089
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Displaying Image from Servlet on J2ME device E-mail
User Rating: / 55
PoorBest 

Example below displays image from Tomcat server on J2ME device.

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

public class ImageMidletServlet extends MIDlet implements CommandListener 
{
    Display display = null;
    Form form = null;
    String url = "http://localhost:8080/servlets-examples/images/DSCF0065.JPG";
    Command backCommand = new Command("Back", Command.BACK, 0);
    Command submitCommand = new Command("Submit", Command.OK, 2);
    Command exitCommand = new Command("Exit", Command.STOP, 3);
    private Test test;

    public ImageMidletServlet() {}

    public void startApp() throws MIDletStateChangeException 
    {
        display = Display.getDisplay(this);
        form = new Form("Show Image");
        form.addCommand(submitCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        display.setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d
    {
        if (c == exitCommand
        {
            destroyApp(true);
            notifyDestroyed();
        }
        else if (c == backCommand
        {
            display.setCurrent(form);
        }
        else if (c == submitCommand
        {
            test  = new Test(this);
            test.start();
        }
    }


    class Test implements Runnable
    {
        ImageMidletServlet midlet;
        private Display display;

        public Test(ImageMidletServlet midlet)
        {
            this.midlet = midlet;
            display = Display.getDisplay(midlet);
        }

        public void start() 
        {
            Thread t = new Thread(this);
            t.start();
        }

        public void run()
        {
            DataInputStream is = null;
            StringBuffer sb = new StringBuffer();
            Image img= null;
            try 
            {
                HttpConnection c = (HttpConnectionConnector.open(url);
                int len = (int)c.getLength();
        
                if (len > 0
                {
                    is = c.openDataInputStream();
                    byte[] data = new byte[len];
                    is.readFully(data);
                    img = Image.createImage(data, 0, len);
                    Form f = new Form("Image");      
                    ImageItem imgItem = new ImageItem("", img, 
                      ImageItem.LAYOUT_NEWLINE_AFTER | 
                      ImageItem.LAYOUT_CENTER, null);   
                      
                    f.append(imgItem);
                    display.setCurrent(f);
                
                else 
                {
                    showAlert("length is null");;
                }
                is.close();
                c.close();
            
            catch (Exception e
            {
                e.printStackTrace();
                showAlert(e.getMessage());
            }
        }
        /* Display Error On screen*/
        private void showAlert(String err
        {
            Alert a = new Alert("");
            a.setString(err);
            a.setTimeout(Alert.FOREVER);
            display.setCurrent(a);
        }
    };
}

 Related Tips


 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.