<strong>/*————————————————–</strong>
<strong>* ViewPngThread.java</strong>
<strong>*</strong>
<strong>* Download and view a png file. The download is</strong>
<strong>* done in the background with a separate thread</strong>
<strong>*</strong>
<strong>* Example from the book: Core J2ME Technology</strong>
<strong>* Copyright John W. Muchow http://www.CoreJ2ME.com</strong>
<strong>* You may use/modify for any non-commercial purpose</strong>
<strong>*————————————————-*/</strong>
<strong>import javax.microedition.midlet.*;</strong>
<strong>import javax.microedition.lcdui.*;</strong>
<strong>import javax.microedition.io.*;</strong>
<strong>import java.io.*;</strong>
<strong>public class ViewPngThread extends MIDlet implements CommandListener</strong>
<strong>{</strong>
<strong>private Display display;</strong>
<strong>private TextBox tbMain;</strong>
<strong>private Alert alStatus;</strong>
<strong>private Form fmViewPng;</strong>
<strong>private Command cmExit;</strong>
<strong>private Command cmView;</strong>
<strong>private Command cmBack;</strong>
<strong>private static final int ALERT_DISPLAY_TIME = 3000;</strong>
<strong>Image im = null;</strong>
<strong>public ViewPngThread()</strong>
<strong>{</strong>
<strong>display = Display.getDisplay(this);</strong>
<strong>// Create the Main textbox with a maximum of 75 characters</strong>
<strong>tbMain = new TextBox(”Enter url”, “http://www.corej2me.com/midpbook_v1e1/ch14/bird.png”, 75, 0);</strong>
<strong>// Create commands and add to textbox</strong>
<strong>cmExit = new Command(”Exit”, Command.EXIT, 1);</strong>
<strong>cmView = new Command(”View”, Command.SCREEN, 2);</strong>
<strong>tbMain.addCommand(cmExit);</strong>
<strong>tbMain.addCommand(cmView );</strong>
<strong>// Set up a listener for textbox</strong>
<strong>tbMain.setCommandListener(this);</strong>
<strong>// Create the form that will hold the png image</strong>
<strong>fmViewPng = new Form(””);</strong>
<strong>// Create commands and add to form</strong>
<strong>cmBack = new Command(”Back”, Command.BACK, 1);</strong>
<strong>fmViewPng.addCommand(cmBack);</strong>
<strong>// Set up a listener for form</strong>
<strong>fmViewPng.setCommandListener(this);</strong>
<strong>}</strong>
<strong>public void startApp()</strong>
<strong>{</strong>
<strong>display.setCurrent(tbMain);</strong>
<strong>}</strong>
<strong>public void pauseApp()</strong>
<strong>{ }</strong>
<strong>public void destroyApp(boolean unconditional)</strong>
<strong>{ }</strong>
<strong>/*————————————————–</strong>
<strong>* Process events</strong>
<strong>*————————————————-*/</strong>
<strong>public void commandAction(Command c, Displayable s)</strong>
<strong>{</strong>
<strong>// If the Command button pressed was “Exit”</strong>
<strong>if (c == cmExit)</strong>
<strong>{</strong>
<strong>destroyApp(false);</strong>
<strong>notifyDestroyed();</strong>
<strong>}</strong>
<strong>else if (c == cmView)</strong>
<strong>{</strong>
<strong>// Show alert indicating we are starting a download.</strong>
<strong>// This alert is NOT modal, it appears for</strong>
<strong>// approximately 3 seconds (see ALERT_DISPLAY_TIME)</strong>
<strong>showAlert(”Downloading”, false, tbMain);</strong>
<strong>// Create an instance of the class that will</strong>
<strong>// download the file in a separate thread</strong>
<strong>Download dl = new Download(tbMain.getString(), this);</strong>
<strong>// Start the thread/download</strong>
<strong>dl.start();</strong>
<strong>}</strong>
<strong>else if (c == cmBack)</strong>
<strong>{</strong>
<strong>display.setCurrent(tbMain);</strong>
<strong>}</strong>
<strong>}</strong>
<strong>/*————————————————–</strong>
<strong>* Called by the thread after attempting to download</strong>
<strong>* an image. If the parameter is ‘true’ the download</strong>
<strong>* was successful, and the image is shown on a form.</strong>
<strong>* If parameter is ‘false’ the download failed, and</strong>
<strong>* the user is returned to the textbox.</strong>
<strong>*</strong>
<strong>* In either case, show an alert indicating the</strong>
<strong>* the result of the download.</strong>
<strong>*————————————————-*/</strong>
<strong>public void showImage(boolean flag)</strong>
<strong>{</strong>
<strong>// Download failed…</strong>
<strong>if (flag == false)</strong>
<strong>{</strong>
<strong>// Alert followed by the main textbox</strong>
<strong>showAlert(”Download Failure”, true, tbMain);</strong>
<strong>}</strong>
<strong>else // Successful download…</strong>
<strong>{</strong>
<strong>ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);</strong>
<strong>// If there is already an image, set (replace) it</strong>
<strong>if (fmViewPng.size() != 0)</strong>
<strong>fmViewPng.set(0, ii);</strong>
<strong>else // Append the image to the empty form</strong>
<strong>fmViewPng.append(ii);</strong>
<strong>// Alert followed by the form holding the image</strong>
<strong>showAlert(”Download Successful”, true, fmViewPng);</strong>
<strong>}</strong>
<strong>}</strong>
<strong>/*————————————————–</strong>
<strong>* Show an alert with the parameters determining</strong>
<strong>* the type (modal or not) and the displayable to</strong>
<strong>* show after the alert is dismissed</strong>
<strong>*————————————————-*/</strong>
<strong>public void showAlert(String msg, boolean modal, Displayable displayable)</strong>
<strong>{</strong>
<strong>// Create alert, add text, associate a sound</strong>
<strong>alStatus = new Alert(”Status”, msg, null, AlertType.INFO);</strong>
<strong>// Set the alert type</strong>
<strong>if (modal)</strong>
<strong>alStatus.setTimeout(Alert.FOREVER);</strong>
<strong>else</strong>
<strong>alStatus.setTimeout(ALERT_DISPLAY_TIME);</strong>
<strong>// Show the alert, followed by the displayable</strong>
<strong>display.setCurrent(alStatus, displayable);</strong>
<strong>}</strong>
<strong>}</strong>
<strong>/*————————————————–</strong>
<strong>* Class – Download</strong>
<strong>*</strong>
<strong>* Download an image file in a separate thread</strong>
<strong>*————————————————-*/</strong>
<strong>class Download implements Runnable</strong>
<strong>{</strong>
<strong>private String url;</strong>
<strong>private ViewPngThread MIDlet;</strong>
<strong>private boolean downloadSuccess = false;</strong>
<strong>public Download(String url, ViewPngThread MIDlet)</strong>
<strong>{</strong>
<strong>this.url = url;</strong>
<strong>this.MIDlet = MIDlet;</strong>
<strong>}</strong>
<strong>/*————————————————–</strong>
<strong>* Download the image</strong>
<strong>*————————————————-*/</strong>
<strong>public void run()</strong>
<strong>{</strong>
<strong>try</strong>
<strong>{</strong>
<strong>getImage(url);</strong>
<strong>}</strong>
<strong>catch (Exception e)</strong>
<strong>{</strong>
<strong>System.err.println(”Msg: ” + e.toString());</strong>
<strong>}</strong>
<strong>}</strong>
<strong>/*————————————————–</strong>
<strong>* Create and start the new thread</strong>
<strong>*————————————————-*/</strong>
<strong>public void start()</strong>
<strong>{</strong>
<strong>Thread thread = new Thread(this);</strong>
<strong>try</strong>
<strong>{</strong>
<strong>thread.start();</strong>
<strong>}</strong>
<strong>catch (Exception e)</strong>
<strong>{</strong>
<strong>}</strong>
<strong>}</strong>
<strong>/*————————————————–</strong>
<strong>* Open connection and download png into a byte array.</strong>
<strong>*————————————————-*/</strong>
<strong>private void getImage(String url) throws IOException</strong>
<strong>{</strong>
<strong>ContentConnection connection = (ContentConnection) Connector.open(url);</strong>
<strong>// * There is a bug in MIDP 1.0.3 in which read() sometimes returns</strong>
<strong>// an invalid length. To work around this, I have changed the</strong>
<strong>// stream to DataInputStream and called readFully() instead of read()</strong>
<strong>// InputStream iStrm = connection.openInputStream();</strong>
<strong>DataInputStream iStrm = connection.openDataInputStream();</strong>
<strong>ByteArrayOutputStream bStrm = null;</strong>
<strong>Image im = null;</strong>
<strong>try</strong>
<strong>{</strong>
<strong>// ContentConnection includes a length method</strong>
<strong>byte imageData[];</strong>
<strong>int length = (int) connection.getLength();</strong>
<strong>if (length != -1)</strong>
<strong>{</strong>
<strong>imageData = new byte[length];</strong>
<strong>// Read the png into an array</strong>
<strong>// iStrm.read(imageData);</strong>
<strong>iStrm.readFully(imageData);</strong>
<strong>}</strong>
<strong>else // Length not available…</strong>
<strong>{</strong>
<strong>bStrm = new ByteArrayOutputStream();</strong>
<strong>int ch;</strong>
<strong>while ((ch = iStrm.read()) != -1)</strong>
<strong>bStrm.write(ch);</strong>
<strong>imageData = bStrm.toByteArray();</strong>
<strong>}</strong>
<strong>// Create the image from the byte array</strong>
<strong>im = Image.createImage(imageData, 0, imageData.length);</strong>
<strong>}</strong>
<strong>finally</strong>
<strong>{</strong>
<strong>// Clean up</strong>
<strong>if (connection != null)</strong>
<strong>connection.close();</strong>
<strong>if (iStrm != null)</strong>
<strong>iStrm.close();</strong>
<strong>if (bStrm != null)</strong>
<strong>bStrm.close();</strong>
<strong>}</strong>
<strong>// Return to the caller the status of the download</strong>
<strong>if (im == null)</strong>
<strong>MIDlet.showImage(false);</strong>
<strong>else</strong>
<strong>{</strong>
<strong>MIDlet.im = im;</strong>
<strong>MIDlet.showImage(true);</strong>
<strong>}</strong>
<strong>}</strong>
<strong>}</strong>
November 1, 2016 pukul 23:34
My brother recommended I might like this web site.
He was entirely right. This post actually made my day.
You can not imagine simply how much time I had spent for this info!
Thanks!
SukaSuka