use of com.codename1.components.FileTreeModel in project CodenameOne by codenameone.
the class CodenameOneImplementation method openGallery.
/**
* Opens the device gallery
* The method returns immediately and the response will be sent asynchronously
* to the given ActionListener Object
*
* use this in the actionPerformed to retrieve the file path
* String path = (String) evt.getSource();
*
* @param response a callback Object to retrieve the file path
* @param type one of the following GALLERY_IMAGE, GALLERY_VIDEO, GALLERY_ALL
* @throws RuntimeException if this feature failed or unsupported on the platform
*/
public void openGallery(final ActionListener response, int type) {
final Dialog d = new Dialog("Select a picture");
d.setLayout(new BorderLayout());
FileTreeModel model = new FileTreeModel(true);
if (type == Display.GALLERY_IMAGE) {
model.addExtensionFilter("jpg");
model.addExtensionFilter("png");
} else if (type == Display.GALLERY_VIDEO) {
model.addExtensionFilter("mp4");
model.addExtensionFilter("3pg");
model.addExtensionFilter("avi");
model.addExtensionFilter("mov");
} else if (type == Display.GALLERY_ALL) {
model.addExtensionFilter("jpg");
model.addExtensionFilter("png");
model.addExtensionFilter("mp4");
model.addExtensionFilter("3pg");
model.addExtensionFilter("avi");
model.addExtensionFilter("mov");
}
FileTree t = new FileTree(model) {
protected Button createNodeComponent(final Object node, int depth) {
if (node == null || !getModel().isLeaf(node)) {
return super.createNodeComponent(node, depth);
}
Hashtable t = (Hashtable) Storage.getInstance().readObject("thumbnails");
if (t == null) {
t = new Hashtable();
}
final Hashtable thumbs = t;
final Button b = super.createNodeComponent(node, depth);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
response.actionPerformed(new ActionEvent(node, ActionEvent.Type.Other));
d.dispose();
}
});
final ImageIO imageio = ImageIO.getImageIO();
if (imageio != null) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
byte[] data = (byte[]) thumbs.get(node);
if (data == null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
imageio.save(FileSystemStorage.getInstance().openInputStream((String) node), out, ImageIO.FORMAT_JPEG, b.getIcon().getWidth(), b.getIcon().getHeight(), 1);
data = out.toByteArray();
thumbs.put(node, data);
Storage.getInstance().writeObject("thumbnails", thumbs);
} catch (IOException ex) {
Log.e(ex);
}
}
Image im = Image.createImage(data, 0, data.length);
b.setIcon(im);
}
});
}
return b;
}
};
d.addComponent(BorderLayout.CENTER, t);
d.placeButtonCommands(new Command[] { new Command("Cancel") });
Command c = d.showAtPosition(2, 2, 2, 2, true);
if (c != null) {
response.actionPerformed(null);
}
}
Aggregations