与Codename One共享表单或容器

时间:2016-02-14 23:23:30

标签: java share codenameone

我想在CN1上分享我的应用程序屏幕,想法是创建一个以JPG格式的屏幕并分享它。

从其他帖子我有这段代码

 @Override
protected void onGpsTracker_Button1Action(Component c, ActionEvent event) {

  String file="toShare.jpeg";
  String path="";
    Image i= Image.createImage(c.getComponentForm().getWidth(),c.getComponentForm().getHeight());
  c.paintComponent(i.getGraphics());
  OutputStream save;
    try {
        save = Storage.getInstance().createOutputStream(file);
        ImageIO.getImageIO().save(i, save, ImageIO.FORMAT_JPEG,(float) 0.90f);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
          String [] entries= Storage.getInstance().listEntries();
   for( int k=0;k< entries.length;k++){

      if (entries[k].startsWith("toShare")){
        path=(String)Storage.getInstance().readObject(entries[k]);}}
   findShareSec().setImageToShare(path, "image/jpeg"); 


}

我找不到最后一行的方法

findShareSec()

因此不保存图像。 保存图像后,我如何分享? 在Android Studio上我使用shareIntent并在每个应用程序上共享

1 个答案:

答案 0 :(得分:2)

我们刚刚在周末之前添加了this exact sample to 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");

以下是上述代码的Java 5版本:

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);
    Util.cleanup(os);
} catch(IOException err) {
    Log.e(err);
}