HelloMidlet.java
-----------------------
package main;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
import com.sun.lwuit.*;
import javax.microedition.midlet.MIDlet;
public class HelloMidlet extends MIDlet {
public static int dwidth;
public static int dheight;
public static javax.microedition.lcdui.Display lcduiDisplay;
public static HelloMidlet midlet;
public void startApp() {
try {
midlet = this;
Display.init(this);
lcduiDisplay = javax.microedition.lcdui.Display.getDisplay(this);
dwidth = Display.getInstance().getDisplayWidth();
dheight = Display.getInstance().getDisplayHeight();
} catch (Exception ex) {
}
ImageClass imgform = new ImageClass();
imgform.showLcduiImageForm(null);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
ImageClass.java
-------------------
package main;
import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
public class ImageClass {
public static Form imgForm;
public void showLcduiImageForm(Object img) {
try {
// Display.init(HelloMidlet.midlet);
imgForm = new Form("Hello World");
Container middleContainer = new Container();
middleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
middleContainer.setUIID("middleContainer");
middleContainer.setScrollableY(true);
middleContainer.getStyle().setBgColor(0XDEDBDE);
//
Label empty2 = new Label("");
empty2.setFocusable(true);
middleContainer.addComponent(empty2);
Button browse = new Button("browse");
browse.setUIID("actionButton");
browse.setHeight(25);
browse.setAlignment(Button.CENTER);
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
ImgBrowser browser = new ImgBrowser();
browser.browseForImage(HelloMidlet.midlet);
}
});
Container cp_details = new Container(new BoxLayout(BoxLayout.X_AXIS));
Image img1 = null;
try {
img1 = Image.createImage(img);
Label l1 = new Label(img1.scaled(30, 30));
cp_details.addComponent(l1);
} catch (Exception ex) {
// Dialog.show("Error", "Error creating Image from lcdui", "OK", null);
}
cp_details.addComponent(browse);
middleContainer.addComponent(cp_details);
imgForm.addComponent(middleContainer);
imgForm.show();
} catch (Exception ex) {
Dialog.show("Exception", "" + ex.getMessage(), "OK", null);
}
}
}
ImgBrowser.java
--------------------
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
public class ImgBrowser implements CommandListener {
private Image dirIcon;
private Image fileIcon;
private Image[] iconList;
private String currDirName;
/*
* separator string as defined by FC specification
*/
private static final String SEP_STR = "/";
/*
* separator character as defined by FC specification
*/
private static final char SEP = '/';
/*
* special string that denotes upper directory accessible by this browser.
* this virtual directory contains all roots.
*/
private static final String MEGA_ROOT = "/";
/*
* special string denotes upper directory
*/
private static final String UP_DIRECTORY = "..";
private Command view = new Command("Select", Command.ITEM, 1);
private Command cancel = new Command("Cancel", Command.ITEM, 2);
private Command back = new Command("Back", Command.BACK, 2);
MIDlet midletObj;
public ImgBrowser() {
currDirName = MEGA_ROOT;
try {
dirIcon = Image.createImage("/dir.png");
} catch (IOException e) {
dirIcon = null;
}
try {
fileIcon = Image.createImage("/file.png");
} catch (IOException e) {
fileIcon = null;
}
iconList = new Image[]{fileIcon, dirIcon};
}
public void browseForImage(MIDlet midlet) {
midletObj = midlet;
try {
showCurrDir();
} catch (SecurityException e) {
Alert alert =
new Alert("Error", "You are not authorized to access the restricted API", null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Form form = new Form("Cannot access FileConnection");
form.append(new StringItem(null,
"You cannot run this MIDlet with the current permissions. "
+ "Sign the MIDlet suite, or run it in a different security domain"));
// form.addCommand(exit);
// form.setCommandListener(this);
Display.getDisplay(midletObj).setCurrent(alert, form);
} catch (Exception e) {
}
}
/**
* Show file list in the current directory .
*/
void showCurrDir() {
Enumeration e;
FileConnection currDir = null;
List browser;
try {
if (MEGA_ROOT.equals(currDirName)) {
e = FileSystemRegistry.listRoots();
browser = new List(currDirName, List.IMPLICIT);
} else {
currDir = (FileConnection) Connector.open("file://localhost/" + currDirName);
e = currDir.list();
browser = new List(currDirName, List.IMPLICIT);
// not root - draw UP_DIRECTORY
browser.append(UP_DIRECTORY, dirIcon);
}
while (e.hasMoreElements()) {
String fileName = (String) e.nextElement();
if (fileName.charAt(fileName.length() - 1) == SEP) {
// This is directory
browser.append(fileName, dirIcon);
} else {
// this is regular file
browser.append(fileName, fileIcon);
}
}
browser.setSelectCommand(view);
browser.addCommand(cancel);
//Do not allow creating files/directories beside root
if (!MEGA_ROOT.equals(currDirName)) {
// browser.addCommand(prop);
// browser.addCommand(creat);
// browser.addCommand(delete);
}
// browser.addCommand(exit);
//
browser.setCommandListener(this);
if (currDir != null) {
currDir.close();
}
Display.getDisplay(midletObj).setCurrent(browser);
} catch (IOException ioe) {
}
}
public void commandAction(Command c, Displayable d) {
if (c == view) {
List curr = (List) d;
final String currFile = curr.getString(curr.getSelectedIndex());
new Thread(new Runnable() {
public void run() {
if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
traverseDirectory(currFile);
} else {
// Show file contents
showFile(currFile);
}
}
}).start();
} else if (c == back) {
showCurrDir();
}
else if (c == cancel) {
com.sun.lwuit.Display.init(midletObj);
ImageClass.imgForm.show();
}
}
void traverseDirectory(String fileName) {
/*
* In case of directory just change the current directory and show it
*/
if (currDirName.equals(MEGA_ROOT)) {
if (fileName.equals(UP_DIRECTORY)) {
// can not go up from MEGA_ROOT
return;
}
currDirName = fileName;
} else if (fileName.equals(UP_DIRECTORY)) {
// Go up one directory
int i = currDirName.lastIndexOf(SEP, currDirName.length() - 2);
if (i != -1) {
currDirName = currDirName.substring(0, i + 1);
} else {
currDirName = MEGA_ROOT;
}
} else {
currDirName = currDirName + fileName;
}
showCurrDir();
}
void showFile(String fileName) {
// Form mForm = new Form("Image");
FileConnection fc = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
fc = (FileConnection) Connector.open("file://localhost/" + currDirName + fileName);
int length = (int) fc.fileSize();//possible loss of precision may throw error
byte[] data = null;
if (length != -1) {
data = new byte[length];
in = new DataInputStream(fc.openInputStream());
in.readFully(data);
} else {
int chunkSize = 512;
int index = 0;
int readLength = 0;
in = new DataInputStream(fc.openInputStream());
data = new byte[chunkSize];
do {
if (data.length < index + chunkSize) {
byte[] newData = new byte[index + chunkSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
}
readLength = in.read(data, index, chunkSize);
index += readLength;
} while (readLength == chunkSize);
length = index;
}
Image image = Image.createImage(data, 0, length);
ImageItem imageItem = new ImageItem(null, image, 0, null);
com.sun.lwuit.Display.init(midletObj);
ImageClass imgForm=new ImageClass();
imgForm.showLcduiImageForm(image);
// mForm.append(imageItem);
// mForm.setTitle("Done.");
// mForm.addCommand(select);
// mForm.addCommand(back);
// mForm.setCommandListener(this);
//fc = (FileConnection) Connector.open("file:///root1/x.PNG");
if (!fc.exists()) {
try {
fc.create();
} catch (Exception ce) {
System.out.print("Create Error: " + ce);
}
}
out = new DataOutputStream(fc.openOutputStream());
out.write(data);
} catch (IOException ioe) {
StringItem stringItem = new StringItem(null, ioe.toString());
// mForm.append(stringItem);
// mForm.addCommand(back);
// mForm.setCommandListener(this);
// mForm.setTitle("Done.");
com.sun.lwuit.Display.init(midletObj);
ImageClass.imgForm.show();
} finally {
try {
if (in != null) {
in.close();
}
if (fc != null) {
fc.close();
}
} catch (IOException ioe) {
}
}
// StringItem stringItem = new StringItem(null, "\nfile://localhost/" + currDirName + fileName);
// mForm.append(stringItem);
// Display.getDisplay(midletObj).setCurrent(mForm);
}
}
icons:
-----------
file.png
dir.png
-----------------------
package main;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
import com.sun.lwuit.*;
import javax.microedition.midlet.MIDlet;
public class HelloMidlet extends MIDlet {
public static int dwidth;
public static int dheight;
public static javax.microedition.lcdui.Display lcduiDisplay;
public static HelloMidlet midlet;
public void startApp() {
try {
midlet = this;
Display.init(this);
lcduiDisplay = javax.microedition.lcdui.Display.getDisplay(this);
dwidth = Display.getInstance().getDisplayWidth();
dheight = Display.getInstance().getDisplayHeight();
} catch (Exception ex) {
}
ImageClass imgform = new ImageClass();
imgform.showLcduiImageForm(null);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
ImageClass.java
-------------------
package main;
import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
public class ImageClass {
public static Form imgForm;
public void showLcduiImageForm(Object img) {
try {
// Display.init(HelloMidlet.midlet);
imgForm = new Form("Hello World");
Container middleContainer = new Container();
middleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
middleContainer.setUIID("middleContainer");
middleContainer.setScrollableY(true);
middleContainer.getStyle().setBgColor(0XDEDBDE);
//
Label empty2 = new Label("");
empty2.setFocusable(true);
middleContainer.addComponent(empty2);
Button browse = new Button("browse");
browse.setUIID("actionButton");
browse.setHeight(25);
browse.setAlignment(Button.CENTER);
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
ImgBrowser browser = new ImgBrowser();
browser.browseForImage(HelloMidlet.midlet);
}
});
Container cp_details = new Container(new BoxLayout(BoxLayout.X_AXIS));
Image img1 = null;
try {
img1 = Image.createImage(img);
Label l1 = new Label(img1.scaled(30, 30));
cp_details.addComponent(l1);
} catch (Exception ex) {
// Dialog.show("Error", "Error creating Image from lcdui", "OK", null);
}
cp_details.addComponent(browse);
middleContainer.addComponent(cp_details);
imgForm.addComponent(middleContainer);
imgForm.show();
} catch (Exception ex) {
Dialog.show("Exception", "" + ex.getMessage(), "OK", null);
}
}
}
ImgBrowser.java
--------------------
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
public class ImgBrowser implements CommandListener {
private Image dirIcon;
private Image fileIcon;
private Image[] iconList;
private String currDirName;
/*
* separator string as defined by FC specification
*/
private static final String SEP_STR = "/";
/*
* separator character as defined by FC specification
*/
private static final char SEP = '/';
/*
* special string that denotes upper directory accessible by this browser.
* this virtual directory contains all roots.
*/
private static final String MEGA_ROOT = "/";
/*
* special string denotes upper directory
*/
private static final String UP_DIRECTORY = "..";
private Command view = new Command("Select", Command.ITEM, 1);
private Command cancel = new Command("Cancel", Command.ITEM, 2);
private Command back = new Command("Back", Command.BACK, 2);
MIDlet midletObj;
public ImgBrowser() {
currDirName = MEGA_ROOT;
try {
dirIcon = Image.createImage("/dir.png");
} catch (IOException e) {
dirIcon = null;
}
try {
fileIcon = Image.createImage("/file.png");
} catch (IOException e) {
fileIcon = null;
}
iconList = new Image[]{fileIcon, dirIcon};
}
public void browseForImage(MIDlet midlet) {
midletObj = midlet;
try {
showCurrDir();
} catch (SecurityException e) {
Alert alert =
new Alert("Error", "You are not authorized to access the restricted API", null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Form form = new Form("Cannot access FileConnection");
form.append(new StringItem(null,
"You cannot run this MIDlet with the current permissions. "
+ "Sign the MIDlet suite, or run it in a different security domain"));
// form.addCommand(exit);
// form.setCommandListener(this);
Display.getDisplay(midletObj).setCurrent(alert, form);
} catch (Exception e) {
}
}
/**
* Show file list in the current directory .
*/
void showCurrDir() {
Enumeration e;
FileConnection currDir = null;
List browser;
try {
if (MEGA_ROOT.equals(currDirName)) {
e = FileSystemRegistry.listRoots();
browser = new List(currDirName, List.IMPLICIT);
} else {
currDir = (FileConnection) Connector.open("file://localhost/" + currDirName);
e = currDir.list();
browser = new List(currDirName, List.IMPLICIT);
// not root - draw UP_DIRECTORY
browser.append(UP_DIRECTORY, dirIcon);
}
while (e.hasMoreElements()) {
String fileName = (String) e.nextElement();
if (fileName.charAt(fileName.length() - 1) == SEP) {
// This is directory
browser.append(fileName, dirIcon);
} else {
// this is regular file
browser.append(fileName, fileIcon);
}
}
browser.setSelectCommand(view);
browser.addCommand(cancel);
//Do not allow creating files/directories beside root
if (!MEGA_ROOT.equals(currDirName)) {
// browser.addCommand(prop);
// browser.addCommand(creat);
// browser.addCommand(delete);
}
// browser.addCommand(exit);
//
browser.setCommandListener(this);
if (currDir != null) {
currDir.close();
}
Display.getDisplay(midletObj).setCurrent(browser);
} catch (IOException ioe) {
}
}
public void commandAction(Command c, Displayable d) {
if (c == view) {
List curr = (List) d;
final String currFile = curr.getString(curr.getSelectedIndex());
new Thread(new Runnable() {
public void run() {
if (currFile.endsWith(SEP_STR) || currFile.equals(UP_DIRECTORY)) {
traverseDirectory(currFile);
} else {
// Show file contents
showFile(currFile);
}
}
}).start();
} else if (c == back) {
showCurrDir();
}
else if (c == cancel) {
com.sun.lwuit.Display.init(midletObj);
ImageClass.imgForm.show();
}
}
void traverseDirectory(String fileName) {
/*
* In case of directory just change the current directory and show it
*/
if (currDirName.equals(MEGA_ROOT)) {
if (fileName.equals(UP_DIRECTORY)) {
// can not go up from MEGA_ROOT
return;
}
currDirName = fileName;
} else if (fileName.equals(UP_DIRECTORY)) {
// Go up one directory
int i = currDirName.lastIndexOf(SEP, currDirName.length() - 2);
if (i != -1) {
currDirName = currDirName.substring(0, i + 1);
} else {
currDirName = MEGA_ROOT;
}
} else {
currDirName = currDirName + fileName;
}
showCurrDir();
}
void showFile(String fileName) {
// Form mForm = new Form("Image");
FileConnection fc = null;
DataInputStream in = null;
DataOutputStream out = null;
try {
fc = (FileConnection) Connector.open("file://localhost/" + currDirName + fileName);
int length = (int) fc.fileSize();//possible loss of precision may throw error
byte[] data = null;
if (length != -1) {
data = new byte[length];
in = new DataInputStream(fc.openInputStream());
in.readFully(data);
} else {
int chunkSize = 512;
int index = 0;
int readLength = 0;
in = new DataInputStream(fc.openInputStream());
data = new byte[chunkSize];
do {
if (data.length < index + chunkSize) {
byte[] newData = new byte[index + chunkSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
}
readLength = in.read(data, index, chunkSize);
index += readLength;
} while (readLength == chunkSize);
length = index;
}
Image image = Image.createImage(data, 0, length);
ImageItem imageItem = new ImageItem(null, image, 0, null);
com.sun.lwuit.Display.init(midletObj);
ImageClass imgForm=new ImageClass();
imgForm.showLcduiImageForm(image);
// mForm.append(imageItem);
// mForm.setTitle("Done.");
// mForm.addCommand(select);
// mForm.addCommand(back);
// mForm.setCommandListener(this);
//fc = (FileConnection) Connector.open("file:///root1/x.PNG");
if (!fc.exists()) {
try {
fc.create();
} catch (Exception ce) {
System.out.print("Create Error: " + ce);
}
}
out = new DataOutputStream(fc.openOutputStream());
out.write(data);
} catch (IOException ioe) {
StringItem stringItem = new StringItem(null, ioe.toString());
// mForm.append(stringItem);
// mForm.addCommand(back);
// mForm.setCommandListener(this);
// mForm.setTitle("Done.");
com.sun.lwuit.Display.init(midletObj);
ImageClass.imgForm.show();
} finally {
try {
if (in != null) {
in.close();
}
if (fc != null) {
fc.close();
}
} catch (IOException ioe) {
}
}
// StringItem stringItem = new StringItem(null, "\nfile://localhost/" + currDirName + fileName);
// mForm.append(stringItem);
// Display.getDisplay(midletObj).setCurrent(mForm);
}
}
icons:
-----------
file.png
dir.png