2016-02-26 58 views
1

我試圖使用share()方法,其中包括圖像,但我無法爲圖像提供正確的路徑。我應該在哪裏放置圖像文件,以及路徑是什麼(放入默認包並嘗試「jar:///myimage.png」不起作用),爲什麼沒有明確記錄?cn1 - 獲取共享圖像的文件路徑()

回答

2

圖像可以被存儲在存儲這是繼窗 C路線:\用戶\ 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"); 
+0

好的,我看到這裏有一個重大的誤解。我有一個圖像,我想要與應用程序(應用程序徽標)打包,並希望它與共享文本共享。我不知道如何引用應用程序中打包的文件的路徑。 – ygesher

+0

你可以內置在主題以及應用程序的默認文件夾,並可以像上面提到的圖像 – tizbn

0

share 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"); 
+0

有沒有辦法從文件夾中拉出文件? – ygesher

+0

您可以從JAR獲取文件,但需要將其保存到FileSystem以在共享代碼中使用它。將文件放在src目錄的根目錄下,並用'Display.getInstance()。getResourceAsStream(「/ myFileName」)打開它;'然後不使用Save並且整個截圖過程只使用'Util.copy(inputStream,outputStream );'。 –