Codename One: Save Image to Storage and create small rounded preview

家住魔仙堡 提交于 2019-12-08 08:40:31

Here is a complete working example of storing and loading an image for anyone who also had trouble:

Saving Image:

        String filePath = Capture.capturePhoto();
        if (filePath != null) {
            try {
                String pathToBeStored = FileSystemStorage.getInstance().getAppHomePath() + System.currentTimeMillis() +  ".jpg");
                Image img = Image.createImage(filePath);
                OutputStream os = FileSystemStorage.getInstance().openOutputStream(pathToBeStored );
                ImageIO.getImageIO().save(img, os, ImageIO.FORMAT_JPEG, 0.9f);
                os.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

Loading Image:

        if(pathToImage != null){
            try {
                Image img = Image.createImage(FileSystemStorage.getInstance().openInputStream(pathToImage));
            } catch(Exception ex){
                Dialog.show("Error", "Error during image loading: " + ex, "OK", null);
            }
        }

Use:

Image img = Image.createImage(Storgage.getInstance().createInputStream("ImageName"));

The image preview is an image not a component so you won't see anything unless you place an image within a component of some sort to show it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!