Hello everyone, I am trying to show a image form the mobile memory on the form, but it does not work ,please help me to see what is the problem.
thanks
public class Slidelet extends MIDlet
{
static Slidelet instance;
SlideEngine engine = null;
InputStream is = null;
byte[] slide;
Image img = null;
private Image image1=null;
private static final int CHUNK_SIZE= 1024;
private Image currentImage = null;
Image photo = null;
public Slidelet()
{
instance = this;
}
public void startApp()
{
engine = new SlideEngine( Display.getDisplay(this ) );
Form f1 = new Form( "Slide 1");
f1.append( "This is slide number 1" );
photo=displayImage();
f1.append(new ImageItem("", photo, ImageItem.LAYOUT_TOP, null));
Form f2 = new Form( "Slide 2");
f2.append( "This is slide number 2" );
Form f3 = new Form( "Slide 3");
f3.append( "This is slide number 3" );
engine.addSlide( f1, 2000 );
engine.addSlide( f2, 2000 );
engine.addSlide( f3, 2000 );
engine.startShow();
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public static void quitApp()
{
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
public Image displayImage()
{
try
{
FileConnection fileConn =
(FileConnection)Connector.open("file:///root1/1.png");
InputStream fis = fileConn.openInputStream();
long overallSize = fileConn.fileSize();
int length = 0;
byte[] imageData = new byte[0];
while (length < overallSize)
{
byte[] data = new byte[CHUNK_SIZE];
int readAmount = fis.read(data, 0, CHUNK_SIZE);
byte[] newImageData = new byte[imageData.length + CHUNK_SIZE];
System.arraycopy(imageData, 0, newImageData, 0, length);
System.arraycopy(data, 0, newImageData, length, readAmount);
imageData = newImageData;
length += readAmount;
}
fis.close();
fileConn.close();
if (length > 0)
{
currentImage = Image.createImage(imageData, 0, length);
}
}
catch (Exception e)
{ }
return currentImage;
}
}
public class SlideEngine
{
Display display;
Vector sequence; // the vector hold the sequence of slides and time
Thread thread;
Timer timer;
/**
* Constructor.
* take Display object as initialize parameters
* @param d
*/
public SlideEngine( Display d )
{
display = d;
sequence = new Vector();
}
/**
* Add a slide to this engine.
* @param slide the slide
* @param t the time duration (in ms) this slide is shown on sreen
*/
public void addSlide( Displayable slide, int t )
{
sequence.addElement( slide );
sequence.addElement( new Integer(t) );
}
/**
* Start the slide show.
* This create a new thread (Timer thread) and display the sequence
*/
public void startShow()
{
timer = new Timer();
thread = new Thread( timer );
thread.start();
}
/**
* End the show.
*/
public void endShow()
{
timer.done = true;
}
/**
* This timer class execute the sequence.
*/
class Timer implements Runnable
{
int time;
boolean done;
public void run()
{
done = false;
int cur = 0; // index to sequence
while ( !done && cur < sequence.size() )
{
Object o = sequence.elementAt( cur );
System.out.println("sequence: "+o);
if ( o instanceof Displayable )
{
// if the sequence element is a Displayable, then show it on screen
Displayable d = (Displayable)o;
display.setCurrent( d );
} else if ( o instanceof Integer )
{
// if the sequence is a Integer, then pause for the duration of this number
time = ((Integer)o).intValue();
try
{
Thread.sleep( time );
} catch (Exception e)
{
}
}
// advance to next sequence element
cur++;
}
} // end of class
}
}

Reply With Quote

