Estou tentando abrir uma imagem apartir de uma pasta do FileSystem, consigo percorrer as pastas até achar a imagem!!!
Mas não estou conseguindo crir a imagem para visualizar.
Tentei passar uma string com o diretorio para criar mas deu erro na minha ultima tentativa tentei criar uma array para depois criar a imagem, mas mesmo assim esta dando erro de nullpointer no InputStream is = this.getClass().getResourceAsStream( filename );
O codigo é esse:
/*
* ServidorMidlet.java
*
* Created on 8 de Maio de 2007, 09:45
*/
package servidor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author humphrey
*/
public class ServidorMidlet extends MIDlet implements CommandListener {
/** Creates a new instance of ServidorMidlet */
public ServidorMidlet() {
initialize();
}
private List list1;
private Command okCommand1;
private Command exitCommand1;
private Alert alert1;
private Command backCommand1;
private Vector order_drives;
private String diretorio = new String();
/** Called by the system to indicate that a command has been invoked on a particular displayable.
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
// Insert global pre-action code here
if (displayable == list1) {
if (command == okCommand1) {
// Insert pre-action code here
// Do nothing
// Insert post-action code here
for(int i = 0 ; i<list1.size() ; i++){
if(list1.isSelected(i)){
diretorio = diretorio.concat(list1.getString(i));
if(isImage(diretorio)){
try{
byte[] img = ReadImageAsByteArray(diretorio);
alert1.setImage(visualizar(img));
Display.getDisplay(this).setCurrent(alert1);
}catch(Exception e){
e.printStackTrace();
}
}else{
GetSubDirector(diretorio);
}
break;
}
}
} else if (command == exitCommand1) {
// Insert pre-action code here
exitMIDlet();
// Insert post-action code here
}
} else if (displayable == alert1) {
if (command == backCommand1) {
// Insert pre-action code here
getDisplay().setCurrent(get_list1());
// Insert post-action code here
}
}
// Insert global post-action code here
}
/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here
getDisplay().setCurrent(get_list1());
// Insert post-init code here
}
/**
* This method should return an instance of the display.
*/
public Display getDisplay() {
return Display.getDisplay(this);
}
/**
* This method should exit the midlet.
*/
public void exitMIDlet() {
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
/** This method returns instance for list1 component and should be called instead of accessing list1 field directly.
* @return Instance for list1 component
*/
public List get_list1() {
if (list1 == null) {
// Insert pre-init code here
list1 = new List(null, Choice.IMPLICIT, new String[0], new Image[0]);
list1.addCommand(get_okCommand1());
list1.addCommand(get_exitCommand1());
list1.setCommandListener(this);
list1.setSelectedFlags(new boolean[0]);
// Insert post-init code here
}
return list1;
}
/** This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly.
* @return Instance for okCommand1 component
*/
public Command get_okCommand1() {
if (okCommand1 == null) {
// Insert pre-init code here
okCommand1 = new Command("Ok", Command.OK, 1);
// Insert post-init code here
}
return okCommand1;
}
/** This method returns instance for exitCommand1 component and should be called instead of accessing exitCommand1 field directly.
* @return Instance for exitCommand1 component
*/
public Command get_exitCommand1() {
if (exitCommand1 == null) {
// Insert pre-init code here
exitCommand1 = new Command("Exit", Command.EXIT, 1);
// Insert post-init code here
}
return exitCommand1;
}
/** This method returns instance for alert1 component and should be called instead of accessing alert1 field directly.
* @return Instance for alert1 component
*/
public Alert get_alert1() {
if (alert1 == null) {
// Insert pre-init code here
alert1 = new Alert(null, "<Enter Text>", null, null);
alert1.addCommand(get_backCommand1());
alert1.setCommandListener(this);
alert1.setTimeout(-2);
// Insert post-init code here
}
return alert1;
}
/** This method returns instance for backCommand1 component and should be called instead of accessing backCommand1 field directly.
* @return Instance for backCommand1 component
*/
public Command get_backCommand1() {
if (backCommand1 == null) {
// Insert pre-init code here
backCommand1 = new Command("Back", Command.BACK, 1);
// Insert post-init code here
}
return backCommand1;
}
public void startApp() {
getRoots();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
private void getRoots() {
Enumeration drives = FileSystemRegistry.listRoots();
order_drives = new Vector();
while(drives.hasMoreElements()) {
String root = (String) drives.nextElement();
order_drives.addElement(root);
System.out.println("\t"+root);
}
int n = 0;
while (n < order_drives.size()){
Image img;
try {
img = Image.createImage("/imagens/st-on.png");
String local = (String)order_drives.elementAt(n);
System.out.println("LOCAL->"+local);
list1.append(local,img);
} catch (IOException ex) {
ex.printStackTrace();
}
n++;
}
}
private void GetSubDirector(String sub) {
try {
FileConnection fc = (FileConnection)
Connector.open("file:///"+sub);
System.out.println
("List of files and directories under "+sub+":");
Enumeration filelist = fc.list("*", true);
list1.deleteAll();
while(filelist.hasMoreElements()) {
String fileName = (String) filelist.nextElement();
fc = (FileConnection)
Connector.open("file:///"+sub + fileName);
if(fc.isDirectory()) {
System.out.println("\tDirectory Name: " + fileName);
} else {
System.out.println
("\tFile Name: " + fileName +
"\tSize: "+fc.fileSize());
}
Image img;
try {
img = Image.createImage("/imagens/st-on.png");
list1.append(fileName,img);
} catch (IOException ex) {
ex.printStackTrace();
}
}
fc.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
private boolean isImage(String diretorio) {
int i = diretorio.length();
int x = i - 3;
int y = i - x;
String JPG = "jpg";
String PNG = "png";
String BMP = "bmp";
String teste = diretorio.substring(x);
if(teste.equalsIgnoreCase(JPG) || teste.equalsIgnoreCase(PNG) ||teste.equalsIgnoreCase(BMP)){
return true;
}else{
return false;
}
}
private Image visualizar(String diretorio) {
try {
Image img;
img = Image.createImage(diretorio);
return img;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
private Image visualizar(byte[] image) {
try {
Image img;
img = Image.createImage(image,0,image.length);
return img;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public byte[] ReadImageAsByteArray (String filename) throws IOException{
byte []buffer = new byte[1024];
String imagem = "/" + filename;
InputStream is = this.getClass().getResourceAsStream( filename );
ByteArrayOutputStream out = new ByteArrayOutputStream();
while (is.read( buffer ) != -1) {
out.write( buffer );
}
return out.toByteArray();
}
}
Se alguem puder dar uma ajuda ai !!!
Valeu!!!