/** * Copyright (c) 2012 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example.ammscamera; import java.io.*; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import javax.microedition.io.file.IllegalModeException; import javax.microedition.lcdui.*; public class ImageSaver implements CommandListener { private class SaverThread implements Runnable { public void run() { try { FileConnection fc = (FileConnection) Connector.open(System.getProperty("fileconn.dir.photos") + filename + "." + format); if (!fc.exists()) { fc.create(); DataOutputStream dout = new DataOutputStream(fc.openDataOutputStream()); dout.write(data); dout.close(); fc.close(); showSave("Save image", filename + " was successfully saved to Gallery-Folder \"Images\"!"); } else if (fc.exists() && !isOverwrite) { showMessage("Replace image", filename + " already exists.\nDo you really want to replace \"" + filename + "\"?"); } else { DataOutputStream dout = new DataOutputStream(fc.openDataOutputStream()); dout.write(data); dout.close(); fc.close(); showSave("Replace image", filename + " was successfully replaced in Gallery-Folder \"Images\"!"); } } catch (IOException e) { //e.printStackTrace(); if (isOverwrite) { showAlert("Replace image", filename + " could not replaced because of " + e.getMessage()); } else { showAlert("Save image", filename + " could not saved because of " + e.getMessage()); } } catch (IllegalModeException ime) { if (isOverwrite) { showAlert("Replace image", filename + " could not replaced because of " + ime.getMessage()); } else { showAlert("Save image", filename + " could not saved because of " + ime.getMessage()); } } catch (SecurityException se) { if (isOverwrite) { showAlert("Replace image", "Please allow your phone to replace " + filename + " in Gallery-Folder \"Images\" and try again!"); } else { showAlert("Save image", "Please allow your phone to save " + filename + " in Gallery-Folder \"Images\" and try again!"); } } catch (IllegalArgumentException iae) { if (isOverwrite) { showAlert("Replace image", filename + " could not replaced because of " + iae.getMessage()); } else { showAlert("Save Image", filename + " could not saved because of " + iae.getMessage()); } } catch (OutOfMemoryError me) { if (isOverwrite) { if (me.getMessage().equals("")) { showAlert("Replace image", filename + " could not replaced because there is insufficient memory"); } else { showAlert("Replace image", filename + " could not replaced because of " + me.getMessage()); } } else if (me.getMessage().equals("")) { showAlert("Save image", filename + " could not saved because there is insufficient memory"); } else { showAlert("Save image", filename + " could not saved because of " + me.getMessage()); } } } private SaverThread() { } SaverThread(SaverThread saverthread) { this(); } } public ImageSaver(CameraMIDlet midlet, byte data[], String format, Displayable cf, Displayable imgf) { ok = new Command("OK", 4, 1); OK = new Command("OK", 4, 1); yes = new Command("Yes", 4, 1); no = new Command("No", 2, 1); back = new Command("Back", 2, 1); this.midlet = midlet; this.data = data; this.format = format; CameraForm = cf; ImageForm = imgf; showFilename(); } private void showFilename() { isOverwrite = false; message = new TextBox("Please enter a file name:", "", 15, 0); message.addCommand(ok); message.addCommand(back); message.setCommandListener(this); Display.getDisplay(midlet).setCurrent(message); } public String getFilename() { return filename; } private void showWaitingForm(String title, String text) { Form waiting = new Form(title); Gauge gauge = new Gauge(null, false, -1, 2); waiting.append(text); waiting.append(gauge); Display.getDisplay(midlet).setCurrent(waiting); } private void showSave(String title, String text) { Alert save = new Alert(title, text, null, AlertType.CONFIRMATION); save.setTimeout(-2); save.addCommand(OK); save.setCommandListener(this); Display.getDisplay(midlet).setCurrent(save); } private void showMessage(String title, String text) { Alert alert = new Alert(title, text, null, AlertType.INFO); alert.setTimeout(-2); alert.addCommand(yes); alert.addCommand(no); alert.setCommandListener(this); Display.getDisplay(midlet).setCurrent(alert); } private void showAlert(String title, String text) { Alert alert = new Alert(title, text, null, AlertType.INFO); alert.setTimeout(-2); alert.addCommand(Alert.DISMISS_COMMAND); alert.setCommandListener(this); Display.getDisplay(midlet).setCurrent(alert); } public String getImageTitle() { return getFilename() + ".jpeg"; } public void commandAction(Command c, Displayable s) { if (c == ok) { filename = message.getString(); showWaitingForm("Save image", "Saving " + filename + " to Images..."); (new Thread(new SaverThread(null))).start(); } if (c == back || c == Alert.DISMISS_COMMAND) { Display.getDisplay(midlet).setCurrent(CameraForm); } if (c == OK) { ImageForm.setTitle(getImageTitle()); Display.getDisplay(midlet).setCurrent(ImageForm); } if (c == yes) { isOverwrite = true; showWaitingForm("Replace image", "Replacing " + filename + " in Images..."); (new Thread(new SaverThread(null))).start(); } if (c == no) { isOverwrite = false; message.setString(filename); Display.getDisplay(midlet).setCurrent(message); } } CameraMIDlet midlet; private TextBox message; String filename; String format; byte data[]; private boolean isOverwrite; private Command ok; private Command OK; private Command yes; private Command no; private Command back; Displayable CameraForm; Displayable ImageForm; }