I have to problem with Images currently:
1) I can't save an Image to Storage because it is not supported to store it directly to the storage. I want users to be able to take a photo with the camera and then the created Photo has to be saved somewhere, so I can retrieve it later on again.
Could you tell me how a) To save an Image b) How to retrieve it
I found this Code piece made by Shai in Stackoverflow where another user asked how to save Images to Storage also.
InputStream stream = FileSystemStorage.getInstance().openInputStream(i);
OutputStream out = Storage.getInstance().createOutputStream("MyImage");
Util.copy(stream, out);
Util.cleanup(stream);
Util.cleanup(out);
But I don't understand it. How can I get an "Image" Object out of this? And how exactly is the "Image" Object stored there? Could you give me a full example here? This is really essential for my app.
2)
This question might be trivial, but I couldn't find an answer to it though. I want to rescale the taken camera picture and make a rounded mask around it. I tried this:
Image capturedImage = Image.createImage(Capture.capturePhoto());
int width = capturedImage.getWidth();
int hight = capturedImage.getHeight();
Image rounded = Image.createImage(width, capturedImage.getHeight(), 0xff000000);
Graphics gr = rounded.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, width, hight, 0, 360);
Object mask = rounded.createMask();
Image ImagePreview = capturedImage.applyMask(mask);
But it did not work out, the ImagePreview wont even show up. This preview would be original-sized anyway, how would I resize it to a small preview for the user afterwards?
Thank you in advance.
Edit:
I have tried the following to save the image:
Image capturedImage = Image.createImage(Capture.capturePhoto());
String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "test.png";
try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
ImageIO.getImageIO().save(capturedImage, os, ImageIO.FORMAT_PNG, 1);
} catch(IOException err) {
Log.e(err);
}
songList.setImage(imageFile);
And to retrieve it later on I tried:
if(songList.getImage() != null){
try {
Image img = Image.createImage(Storage.getInstance().createInputStream(songList.getImage()));
Label test = new Label();
test.setIcon(img);
listCont.add(test);
} catch(Exception ex){
Dialog.show("Error", "Error during image loading: " + ex, "OK", null);
}
}
But I am getting an error that he can't find the file. (Device Simulator). Am I doing something wrong?
Here is a complete working example of storing and loading an image for anyone who also had trouble:
Saving Image:
Loading Image: