【发布时间】:2016-02-26 12:20:51
【问题描述】:
我正在尝试使用 share() 方法,包括一个图像,但我无法为图像提供正确的路径。我应该把图片文件放在哪里,路径是什么(放入默认包并尝试“jar:///myimage.png”不起作用),为什么没有明确记录?
【问题讨论】:
标签: share codenameone
我正在尝试使用 share() 方法,包括一个图像,但我无法为图像提供正确的路径。我应该把图片文件放在哪里,路径是什么(放入默认包并尝试“jar:///myimage.png”不起作用),为什么没有明确记录?
【问题讨论】:
标签: share codenameone
图像可以存储在跟随窗口路径的存储中 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");
【讨论】:
共享 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");
【讨论】:
Display.getInstance().getResourceAsStream("/myFileName"); 打开它,然后不要使用保存,整个屏幕截图过程只需使用Util.copy(inputStream, outputStream);。