在Codename One中共享图像时,收到的消息中始终缺少图像

时间:2016-06-26 19:45:41

标签: codenameone sharing

我正在生成一个保存在FileSytemStorage.getAppHomePath()目录中的图像。我现在需要通过电子邮件,短信分享...这就是为什么我在我的操作方法中使用以下代码(基于Codename One documentation):

    long time = new Date().getTime();

    String fullOutputPath = FileSystemStorage.getInstance().getAppHomePath()
            + "Montage_" + Long.toString(time) + ".png";

    // Save the image with the ImageIO class
    try (OutputStream os = FileSystemStorage.getInstance().openOutputStream(fullOutputPath)){
        ImageIO.getImageIO().save(montage.getMontageImage(), os, ImageIO.FORMAT_PNG, 1.0f);

        }


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        // Enable image sharing (outside the try/catch so that the outputstream in closed for sure)
        if (FileSystemStorage.getInstance().exists(fullOutputPath)) {
            Dialog.show("Saved", "Photo collage saved to " + fullOutputPath
                    + " (file size = " + FileSystemStorage.getInstance().getLength(fullOutputPath) +" B)", "OK", null);
            //Photo collage saved to file://home/Montage_14669... .png (file size = 50387B)

            findValidateMontageShareButton3().setImageToShare(fullOutputPath, "image/png");
            // Null pointer exception

因此,我得到一个NPE,如果我不测试文件是否存在,则没有NPE但图像仍然缺失(模拟器和设备上都有)。

堆栈跟踪如下:

java.lang.NullPointerException
at userclasses.StateMachine.onPage3_ValidateMontageShareButton3Action(StateMachine.java:852)
at generated.StateMachineBase.handleComponentAction(StateMachineBase.java:757)
at com.codename1.ui.util.UIBuilder$FormListener.actionPerformed(UIBuilder.java:2835)
at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:362)
at com.codename1.ui.Button.fireActionEvent(Button.java:411)
at com.codename1.ui.Button.released(Button.java:442)
at com.codename1.ui.Button.pointerReleased(Button.java:530)
at com.codename1.ui.Form.pointerReleased(Form.java:2627)
at com.codename1.ui.Form.pointerReleased(Form.java:2563)
at com.codename1.ui.Component.pointerReleased(Component.java:3158)
at com.codename1.ui.Display.handleEvent(Display.java:2025)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1067)
at com.codename1.ui.Display.mainEDTLoop(Display.java:996)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

看起来我的应用正在生成的文件无法访问共享应用。我是否必须按照建议here for Android添加任何额外的权限?

请注意:我不知道它是否与此问题有关,但我无法再从Eclipse访问Codename One Settings菜单(可能自升级到CN1 lib v 115)

任何帮助表示赞赏,

干杯

1 个答案:

答案 0 :(得分:1)

所以这是在模拟器中工作的答案的一部分(即图像出现在假电子邮件客户端=>见下图)。

Image in the fake email client

因此,似乎无法在操作方法中设置共享按钮(即,当用户单击共享按钮时触发的方法)。它必须先设置。

因此,在beforeShow方法中,我的代码如下:`

    FontImage.setMaterialIcon(findValidateMontageShareButton3(), FontImage.MATERIAL_CHECK_CIRCLE);


   final long time = new Date().getTime();

   // We generate the montage filename JPG otherwise it cannot be sent
   montage.setMontageFullPath(FileSystemStorage.getInstance().getAppHomePath()
            + "Montage_" + Long.toString(time) + ".jpg");

   // We assign the montage filename to the share button BEFORE we can click the button (otherwise the
   // filename cannot be taken into account)
   findValidateMontageShareButton3(f).setImageToShare(
        montage.getMontageFullPath(), "image/jpeg");

然后在与共享按钮相关的onAction方法中,代码为:

    // Save the image with the ImageIO class
    // We wait until the file is completely written to continue
    Display.getInstance().invokeAndBlock(new Runnable() {

        @Override
        public void run() {
            try (OutputStream os = FileSystemStorage.getInstance().openOutputStream(montage.getMontageFullPath())){
                ImageIO.getImageIO().save(montage.getMontageImage(), os, ImageIO.FORMAT_JPEG, 1);   

            } catch (IOException e) {
                Dialog.show("Erreur", "Impossible de sauvegarder le montage! Merci de vérifier l'espace disque disponible.", null, "OK" );
            }
        }
    });

我测试了它并且它在模拟器上工作但在设备上没有。使用png或jpeg文件时,该文件无法附加到SMS或电子邮件中(Android错误消息“异常文件,无法附加文件”)。

但是,如果我第二次这样做,那么可以附加文件。所以现在图像没有丢失,但它无法附加(第一次)仍然令人尴尬。