Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 16:11, 24 November 2013 by lpvalente (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Como ler uma imagem da Galeria em Java ME

From Wiki
Jump to: navigation, search
Article Metadata

Artigo
Tradução:
Por maiconherverton
Última alteração feita por lpvalente em 24 Nov 2013

Resumo

Este artigo mostra como ler uma imagem da Galeria usando a API FileConnection (JSR-75) e mostrá-lo no formulário. As pastas de fotos podem ser acessadas usando as propriedades do sistema, por exemplo: System.getProperty("fileconn.dir.photos"). Conteúdo do arquivo é lido usando InputStream. Os dados são salvos em um array de bytes e o objeto de imagem é criado usando o método Image.createImage().

O código fonte completo para um teste MIDlet:

Source code: ReadMIDlet.java

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.Connector;
import java.io.InputStream;
import java.io.IOException;
 
public class ReadMIDlet extends MIDlet implements CommandListener {
private Form form;
private String photos = "fileconn.dir.photos";
private Image image;
private Command exitCommand;
 
public void startApp() {
form = new Form("ReadMIDlet");
exitCommand = new Command("Exit", Command.EXIT, 1);
form.addCommand(exitCommand);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
String path = System.getProperty(photos); // Por exemplo: file:///C:/Data/Images/
form.append("path = " + path);
// A imagem "image.png" deve estar na mesma pasta acima
image = readFile(path + "image.png");
if (image != null) form.append(image);
else form.append("Image not found!");
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
public Image readFile(String path) {
try {
//abre o arquivo com permissão de leitura
FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
if(!fc.exists()) { //verifica se o arquivo não existe
System.out.println("File doesn't exist!");
}
else { //
int size = (int)fc.fileSize();
InputStream is = fc.openInputStream();
byte bytes[] = new byte[size];
is.read(bytes, 0, size);
image = Image.createImage(bytes, 0, size);
}
 
} catch (IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
} catch (IllegalArgumentException iae) {
System.out.println("IllegalArgumentException: "+iae.getMessage());
}
return image;
}
 
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) this.notifyDestroyed();
}
}

Veja também

This page was last modified on 24 November 2013, at 16:11.
201 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×