Hi again pingkan paula,
Here is the thread safe version of corej2me's pngviewer:
/*--------------------------------------------------
| PngViewer.java
|
| Download and view a png file
|
| Web:
www.corej2me.com |
| Version: 1.0 - Original Release
| Version: 1.1 - Updated getImage() to use
| readFully() and also support reading
| one character at a time when length
| is not available
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
/*--------------------------------------------------
* Main class definition
*-------------------------------------------------*/
public class PngViewer extends MIDlet implements CommandListener, Runnable {
private Display display;
private TextBox tbMain;
private Form fmViewPng;
private Command cmExit;
private Command cmView;
private Command cmdBack;
/*--------------------------------------------------
* Constructor for the class.
*-------------------------------------------------*/
public PngViewer() {
display = Display.getDisplay(this);
// Create the Main textbox with a maximum of 50 characters
tbMain = new TextBox("Enter png url", "http://www.corej2me.com/scratch/spin.png", 50, 0);
// Create commands and add to textbox
cmView = new Command("View", Command.SCREEN, 1);
cmExit = new Command("Exit", Command.SCREEN, 1);
tbMain.addCommand(cmView );
tbMain.addCommand(cmExit);
// Set up a listener to "capture" button presses
tbMain.setCommandListener(this);
// ---------------------------------------
// Create the form that will hold the png image
fmViewPng = new Form("Png Image");
// Create commands and add to form
cmdBack = new Command("Back", Command.BACK, 1);
fmViewPng.addCommand(cmdBack);
// Set up a listener to "capture" button presses
fmViewPng.setCommandListener(this);
}
// -----------------------------------------------------------------------------------------------
/*--------------------------------------------------
* Called by the Application Manager on the device
* to start the MIDlet.
*-------------------------------------------------*/
public void startApp() {
// Display the Main textbox
display.setCurrent(tbMain);
}
// -----------------------------------------------------------------------------------------------
public void pauseApp() {
}
// -----------------------------------------------------------------------------------------------
public void destroyApp(boolean unconditional) {
}
// -----------------------------------------------------------------------------------------------
/*--------------------------------------------------
* Process events
*-------------------------------------------------*/
public void commandAction(Command c, Displayable s) {
// If the Command button pressed was "Exit"
if (c == cmExit) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmView) // If the Command button pressed was "View"
{
// Remove anything that may be on the form
if (fmViewPng.size() > 0)
for (int i = 0; i < fmViewPng.size(); i++)
fmViewPng.delete(i);
Thread t = new Thread(this);
t.start();
} else if (c == cmdBack) // If the Command button pressed was "Back"
display.setCurrent(tbMain);
}
// -----------------------------------------------------------------------------------------------
public void run(){
try {
// Get the image from the web and append to the form
Image im;
if ((im = getImage(tbMain.getString())) != null)
fmViewPng.append(im);
// Display the form with the image
display.setCurrent(fmViewPng);
} catch (Exception e) {
System.err.println("Msg: " + e.toString());
}
}
// -----------------------------------------------------------------------------------------------
/*--------------------------------------------------
* Open an http connection and download a png file
* into a byte array.
*-------------------------------------------------*/
private Image getImage(
String url) throws IOException {
ContentConnection connection = (ContentConnection) Connector.open(url);
DataInputStream iStrm = connection.openDataInputStream();
Image im = null;
try {
// ContentConnection includes a length method
byte imageData[];
int length = (int) connection.getLength();
if (length != -1) {
imageData = new byte[length];
// Read the png into an array
iStrm.readFully(imageData);
} else // Length not available...
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
}
// Create the image from the byte array
im = Image.createImage(imageData, 0, imageData.length);
} finally {
// Clean up
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
}
return (im == null ? null : im);
}
// -----------------------------------------------------------------------------------------------
}
cheers!