View PNG Image with Thread


<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&gt;

<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>

&nbsp;

<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>

&nbsp;

<strong>Image im = null;</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>public ViewPngThread()</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>display = Display.getDisplay(this);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Create the Main textbox with a maximum of 75 characters</strong>

&nbsp;

<strong>tbMain = new TextBox(”Enter url”, “http://www.corej2me.com/midpbook_v1e1/ch14/bird.png”, 75, 0);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Create commands and add to textbox</strong>

&nbsp;

<strong>cmExit = new Command(”Exit”, Command.EXIT, 1);</strong>

&nbsp;

<strong>cmView = new Command(”View”, Command.SCREEN, 2);</strong>

&nbsp;

<strong>tbMain.addCommand(cmExit);</strong>

&nbsp;

<strong>tbMain.addCommand(cmView );</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Set up a listener for textbox</strong>

&nbsp;

<strong>tbMain.setCommandListener(this);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Create the form that will hold the png image</strong>

&nbsp;

<strong>fmViewPng = new Form(””);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Create commands and add to form</strong>

&nbsp;

<strong>cmBack = new Command(”Back”, Command.BACK, 1);</strong>

&nbsp;

<strong>fmViewPng.addCommand(cmBack);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Set up a listener for form</strong>

&nbsp;

<strong>fmViewPng.setCommandListener(this);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>public void startApp()</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>display.setCurrent(tbMain);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>public void pauseApp()</strong>

&nbsp;

<strong>{ }</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>public void destroyApp(boolean unconditional)</strong>

&nbsp;

<strong>{ }</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Process events</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>public void commandAction(Command c, Displayable s)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// If the Command button pressed was “Exit”</strong>

&nbsp;

<strong>if (c == cmExit)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>destroyApp(false);</strong>

&nbsp;

<strong>notifyDestroyed();</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>else if (c == cmView)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// Show alert indicating we are starting a download.</strong>

&nbsp;

<strong>// This alert is NOT modal, it appears for</strong>

&nbsp;

<strong>// approximately 3 seconds (see ALERT_DISPLAY_TIME)</strong>

&nbsp;

<strong>showAlert(”Downloading”, false, tbMain);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Create an instance of the class that will</strong>

&nbsp;

<strong>// download the file in a separate thread</strong>

&nbsp;

<strong>Download dl = new Download(tbMain.getString(), this);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Start the thread/download</strong>

&nbsp;

<strong>dl.start();</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>else if (c == cmBack)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>display.setCurrent(tbMain);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Called by the thread after attempting to download</strong>

&nbsp;

<strong>* an image. If the parameter is ‘true’ the download</strong>

&nbsp;

<strong>* was successful, and the image is shown on a form.</strong>

&nbsp;

<strong>* If parameter is ‘false’ the download failed, and</strong>

&nbsp;

<strong>* the user is returned to the textbox.</strong>

&nbsp;

<strong>*</strong>

&nbsp;

<strong>* In either case, show an alert indicating the</strong>

&nbsp;

<strong>* the result of the download.</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>public void showImage(boolean flag)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// Download failed…</strong>

&nbsp;

<strong>if (flag == false)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// Alert followed by the main textbox</strong>

&nbsp;

<strong>showAlert(”Download Failure”, true, tbMain);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>else // Successful download…</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// If there is already an image, set (replace) it</strong>

&nbsp;

<strong>if (fmViewPng.size() != 0)</strong>

&nbsp;

<strong>fmViewPng.set(0, ii);</strong>

&nbsp;

<strong>else // Append the image to the empty form</strong>

&nbsp;

<strong>fmViewPng.append(ii);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Alert followed by the form holding the image</strong>

&nbsp;

<strong>showAlert(”Download Successful”, true, fmViewPng);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Show an alert with the parameters determining</strong>

&nbsp;

<strong>* the type (modal or not) and the displayable to</strong>

&nbsp;

<strong>* show after the alert is dismissed</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>public void showAlert(String msg, boolean modal, Displayable displayable)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// Create alert, add text, associate a sound</strong>

&nbsp;

<strong>alStatus = new Alert(”Status”, msg, null, AlertType.INFO);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Set the alert type</strong>

&nbsp;

<strong>if (modal)</strong>

&nbsp;

<strong>alStatus.setTimeout(Alert.FOREVER);</strong>

&nbsp;

<strong>else</strong>

&nbsp;

<strong>alStatus.setTimeout(ALERT_DISPLAY_TIME);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Show the alert, followed by the displayable</strong>

&nbsp;

<strong>display.setCurrent(alStatus, displayable);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Class – Download</strong>

&nbsp;

<strong>*</strong>

&nbsp;

<strong>* Download an image file in a separate thread</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>class Download implements Runnable</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>private String url;</strong>

&nbsp;

<strong>private ViewPngThread MIDlet;</strong>

&nbsp;

<strong>private boolean downloadSuccess = false;</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>public Download(String url, ViewPngThread MIDlet)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>this.url = url;</strong>

&nbsp;

<strong>this.MIDlet = MIDlet;</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Download the image</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>public void run()</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>try</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>getImage(url);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>catch (Exception e)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>System.err.println(”Msg: ” + e.toString());</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Create and start the new thread</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>public void start()</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>Thread thread = new Thread(this);</strong>

&nbsp;

<strong>try</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>thread.start();</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>catch (Exception e)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>/*————————————————–</strong>

&nbsp;

<strong>* Open connection and download png into a byte array.</strong>

&nbsp;

<strong>*————————————————-*/</strong>

&nbsp;

<strong>private void getImage(String url) throws IOException</strong>

&nbsp;

<strong>{</strong>

&nbsp;

&nbsp;

<strong>ContentConnection connection = (ContentConnection) Connector.open(url);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// * There is a bug in MIDP 1.0.3 in which read() sometimes returns</strong>

&nbsp;

<strong>// an invalid length. To work around this, I have changed the</strong>

&nbsp;

&nbsp;

<strong>// stream to DataInputStream and called readFully() instead of read()</strong>

&nbsp;

<strong>// InputStream iStrm = connection.openInputStream();</strong>

&nbsp;

<strong>DataInputStream iStrm = connection.openDataInputStream();</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>ByteArrayOutputStream bStrm = null;</strong>

&nbsp;

<strong>Image im = null;</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>try</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// ContentConnection includes a length method</strong>

&nbsp;

<strong>byte imageData[];</strong>

&nbsp;

<strong>int length = (int) connection.getLength();</strong>

&nbsp;

<strong>if (length != -1)</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>imageData = new byte[length];</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Read the png into an array</strong>

&nbsp;

<strong>// iStrm.read(imageData);</strong>

&nbsp;

<strong>iStrm.readFully(imageData);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>else // Length not available…</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>bStrm = new ByteArrayOutputStream();</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>int ch;</strong>

&nbsp;

<strong>while ((ch = iStrm.read()) != -1)</strong>

&nbsp;

<strong>bStrm.write(ch);</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>imageData = bStrm.toByteArray();</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Create the image from the byte array</strong>

&nbsp;

<strong>im = Image.createImage(imageData, 0, imageData.length);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>finally</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>// Clean up</strong>

&nbsp;

<strong>if (connection != null)</strong>

&nbsp;

<strong>connection.close();</strong>

&nbsp;

<strong>if (iStrm != null)</strong>

&nbsp;

<strong>iStrm.close();</strong>

&nbsp;

<strong>if (bStrm != null)</strong>

&nbsp;

<strong>bStrm.close();</strong>

&nbsp;

<strong>}</strong>

&nbsp;

&nbsp;

&nbsp;

<strong>// Return to the caller the status of the download</strong>

&nbsp;

<strong>if (im == null)</strong>

&nbsp;

<strong>MIDlet.showImage(false);</strong>

&nbsp;

<strong>else</strong>

&nbsp;

<strong>{</strong>

&nbsp;

<strong>MIDlet.im = im;</strong>

&nbsp;

<strong>MIDlet.showImage(true);</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

&nbsp;

<strong>}</strong>

Ditulis dalam Ngedidik. 1 Comment »

Satu Tanggapan to “View PNG Image with Thread”

  1. Penumbuh Rambut botak Says:

    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!

    Suka


Tinggalkan Balasan ke Penumbuh Rambut botak Batalkan balasan

Mari kita bersama meningkatkan kesadaran kita - Mulailah berpikir cerdas, Kita semua beragama !!! Agama bukan identitas...jadikanlah agama untuk keluar dari kebodohan

Tulisan di blog ini mungkin sangat ngawur tapi mungkin juga benar. Merdekakan pikiran anda, sentuh hati nurani anda. Yang ada tinggal KASUNYATAN SEJATI

Zoemalang's community at www. zoemalang.wordpress.com

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Tulisan di blog ini mungkin sangat ngawur tapi mungkin juga benar. Merdekakan pikiran anda, sentuh hati nurani anda. Yang ada tinggal KASUNYATAN SEJATI