Saving an Image Locally in Codename One Project
Asked Answered
F

1

7

I've followed the tutorial of creating a camera capture page in this video: http://www.youtube.com/watch?v=nF4eqzVcsic

So my code at the moment looks like this:

protected void onCamera_CaptureButtonAction(Component c, ActionEvent event) {
    String i = Capture.capturePhoto();
    if (i != null) {
        try {
            Image img = Image.createImage(i).scaledHeight(500);
            findCameraLabel().setIcon(img);

        } catch (Exception ex) {
        }
    }

}

I had a look at the CameraDemo application, but can't seem to locate any files being saved.

I basically just want any pictures taken to be saved in the src folder.

Any help would be greatly appreciated. Ari

Fronton answered 21/8, 2013 at 16:26 Comment(0)
A
7

The src folder doesn't exist on your device and you don't have access to the "application folder" (where the native binaries are stored) otherwise you would be able to change your application on the device potentially installing a virus.

The variable i in your example is a temporary file URL that you can see on your PC/Mac. You should copy it to a local file or to local storage.

You can open an input stream to the image using FileSystemStorage, you can then store it using that same class (e.g. in the application home directory) or you can use the Storage class to save the image somewhere.

E.g. you can copy image to local storage as such:

InputStream stream = FileSystemStorage.getInstance().openInputStream(i);
OutputStream out = Storage.getInstance().createOutputStream("MyImage");
Util.copy(stream, out);
Util.cleanup(stream);
Util.cleanup(out);
Atronna answered 22/8, 2013 at 5:8 Comment(4)
Woah awesome. OK thanks. So is it possible to read these files again once they've been saved?Fronton
What's the point of saving them otherwise ;-) Storage has a method to open an input stream too and you can use EncodedImage.create(stream) to get an image object.Atronna
Is this "local storage" something like the LocalStorage of a browser or is it actually persisting the image on the device? If latter, where will it be saved? Can I view it e.g. in the device' photo album or something?Backdrop
No to both. I can go into details but its a long explanation and moderators would "flip". Shortform: storage is permanent but when you uninstall an app its always removed with the uninstall. FileSystem "sometimes" does that and has a more elaborate hierarchy.Atronna

© 2022 - 2024 — McMap. All rights reserved.