【问题标题】:cn1 - get file path to image for share()cn1 - 为 share() 获取图像的文件路径
【发布时间】:2016-02-26 12:20:51
【问题描述】:

我正在尝试使用 share() 方法,包括一个图像,但我无法为图像提供正确的路径。我应该把图片文件放在哪里,路径是什么(放入默认包并尝试“jar:///myimage.png”不起作用),为什么没有明确记录?

【问题讨论】:

    标签: share codenameone


    【解决方案1】:

    图像可以存储在跟随窗口路径的存储中 C:\Users\userName.cn1

    可以使用以下代码读取图像

    InputStream is =  Storage.getInstance().createInputStream("tizbn.JPG");
    EncodedImage i = EncodedImage.create(is, is.available());
    

    从默认文件夹加载图片

    Image i =EncodedImage.create("/tizbn.png");
    

    从主题加载图片

    EncodedImage current = (EncodedImage)  fetchResourceFile().getImage("tizbn.png");
    

    【讨论】:

    • 好的,我知道这里有一个严重的误解。我有一张想要与应用程序一起打包的图像(应用程序徽标),并希望将其与共享文本共享。我不知道如何引用应用程序中打包的文件的路径。
    • 你可以内置主题以及应用程序的默认文件夹,并且可以如上所述读取图像
    【解决方案2】:

    共享 API 适用于 https://www.codenameone.com/javadoc/com/codename1/io/FileSystemStorage.html[FileSystemStorage] 而不适用于 https://www.codenameone.com/javadoc/com/codename1/io/Storage.html[Storage]。

    您需要将文件保存到文件系统路径中,该路径始终为绝对路径,我们建议使用应用主页来存储文件。 developer guide section on the ShareButton 中有一个示例涵盖了这一点:

    Form hi = new Form("ShareButton");
    ShareButton sb = new ShareButton();
    sb.setText("Share Screenshot");
    hi.add(sb);
    
    Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight());
    hi.revalidate();
    hi.setVisible(true);
    hi.paintComponent(screenshot.getGraphics(), true);
    
    String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png";
    try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
        ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1);
    } catch(IOException err) {
        Log.e(err);
    }
    sb.setImageToShare(imageFile, "image/png");
    

    【讨论】:

    • 无法从jar中拉取文件?
    • 您可以从 JAR 中获取文件,但您需要将其保存到 FileSystem 以在共享代码中使用它。将文件放在 src 目录的根目录下并使用Display.getInstance().getResourceAsStream("/myFileName"); 打开它,然后不要使用保存,整个屏幕截图过程只需使用Util.copy(inputStream, outputStream);。
    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2013-03-04
    • 2021-02-05
    • 1970-01-01
    • 2014-02-12
    • 2019-03-28
    相关资源
    最近更新 更多