使用Gluon ShareService共享多个文件(image和txt) [英] Sharing multiple files with Gluon ShareService (image and txt)

查看:79
本文介绍了使用Gluon ShareService共享多个文件(image和txt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想知道如何使用Gluon ShareService共享多个文件(图像和文本文件)。特别是如何与PictureService共享以前拍摄和存储(在图库中)的图像。

We want to know how we can share multiple files (image and txt file) with the Gluon ShareService. Especially how to share an image which was previously taken and stored (in gallery) with the PictureService.

但我们需要首先使用路径和图像名称创建文件。不幸的是,PictureService会保存图像,图像标题包含拍摄照片时的日期和时间。

But we need to create a file first with the path and image name. Unfortunately, the PictureService saves the image with the image title consisting of date and time at the moment the picture was taken.

我们尝试使用loadImageFromGallery方法获取图像名称但是这会返回无效并打开最近的屏幕。

We tried to get the image name with the loadImageFromGallery method but this returns void and opens the recent-screen.

这里我们试图分享一个图像:

Here what we've tried to share an image:

public void sharePicture() {
    Services.get(PicturesService.class).ifPresent(picturesService -> {
          Image image = picturesService.loadImageFromGallery().get();
      File file= new File("Pictures", image.toString());
      Services.get(ShareService.class).ifPresent(service -> {
        service.share("image/jpg", file);
      });
    });
  }




  • 我们如何将图像存储在我们想要的位置我们想要的头衔?

  • 我们如何共享文件和图像?

  • 推荐答案

    您走在正确的轨道上,结合了 Charm Down 的不同服务,为了从图库中选择一个图像并分享它。

    You are on the right track, combining different services from Charm Down, in order to select an image from the gallery and share it.

    这种方法存在一个主要问题:你无法轻易转换JavaFX 图像进入文件

    There is a major problem in this approach, though: You can't convert easily a JavaFX Image into a File.

    到目前为止 PicturesService 只返回一个JavaFX Image,而不是File,因此我们需要一种方法将该图像保存到我们可以读取和共享的文件中。

    So far the PicturesService returns only a JavaFX Image, and not a File, so we need a way to save that image into a file that we can read and share.

    这个过程并不容易,因为在移动设备上我们没有 SwingUtilities

    And the process is not easy since on mobile we don't have SwingUtilities.

    使用 PixelReader 来读取图像并获取字节数组的初始方法并不真正起作用,因为它会为您提供一个大的原始文件无法阅读或分享。

    The initial approach of using a PixelReader to read the image and get a byte array doesn't really work, as it will give you a big raw file that can't be read or share.

    我已使用此解决方案利用PNG编码器从JavaFX图像中获取png的字节数组:

    I've used this solution that makes use of a PNG encoder to get the byte array of a png from a JavaFX image:

    PngEncoderFX encoder = new PngEncoderFX(image, true);
    byte[] bytes = encoder.pngEncode();
    

    然后我将该字节数组保存到公共存储文件夹中的文件中(因此它可以是共享),我可以使用`StorageService检索:

    Then I'll save that byte array into a file in the public storage folder (so it can be shared), that I can retrieve using the `StorageService:

    private File getImageFile(Image image) {
        if (image == null) {
            return null;
        }
    
        // 1. Encode image to png
        PngEncoderFX encoder = new PngEncoderFX(image, true);
        byte[] bytes = encoder.pngEncode();
    
        // 2.Write byte array to a file in public storage 
        File root = Services.get(StorageService.class)
                .flatMap(storage -> storage.getPublicStorage("Pictures"))
                .orElse(null);
        if (root != null) {
            File file = new File(root, "Image-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("uuuuMMdd-HHmmss")) + ".png");
            try (FileOutputStream fos = new FileOutputStream(file)) {
                fos.write(bytes);
                return file;
            } catch (IOException ex) {
                System.out.println("Error: " + ex);
            }
        }
        return null;
    }
    

    现在,您可以调用 PicturesService ,检索图像,将其保存到文件中,最后分享它:

    Now, you can call the PicturesService, retrieve the image, save it to the file and finally share it:

    Services.get(PicturesService.class).ifPresent(pictures -> {
        // 1. Retrieve picture from gallery
        pictures.loadImageFromGallery().ifPresent(image -> {
            // 2. Convert image to file
            File imageFile = getImageFile(image);
    
            // 3. Share file
            if (imageFile != null) {
                Services.get(ShareService.class).ifPresent(share -> {
                    share.share("image/png", imageFile);
                });
            }
        });
    });
    

    请注意,如果您尝试编码大图像,可能会遇到内存问题。

    Note that you may run into memory issues if you try to encode big images.

    无论如何,如果PicturesService首先返回一个文件,那么所有过程都可以简化。如果您想提出问题,可以这里

    Anyway, all the process could be simplified if the PicturesService will return a file in the first place. If you want to file an issue, you can do it here.

    编辑

    避免记忆的可能解决方案问题,并减少共享文件的大小,并基于此解决方案,缩小原始图像,如果它超出了一定的大小,就像它已经在PicturesService的iOS实现中完成一样:

    A possible solution to avoid memory issues, and to reduce the size of the shared file, and based on this solution, is scaling down the original image, if it exceeds certain size, like it is already done in the iOS implementation of the PicturesService:

    private Image scaleImage(Image source) {
        // Possible limit based on memory limitations
        double maxResolution = 1280; 
    
        double width = source.getWidth();
        double height = source.getHeight();
        double targetWidth = width;
        double targetHeight = height;
        if (width > maxResolution || height > maxResolution) {
            double  ratio = width/height;
            if (ratio > 1) {
                targetWidth = maxResolution;
                targetHeight = targetWidth/ ratio;
            }
            else {
                targetHeight = maxResolution;
                targetWidth = targetHeight * ratio;
            }
        }
    
        ImageView imageView = new ImageView(source);
        imageView.setPreserveRatio(true);
        imageView.setFitWidth(targetWidth);
        imageView.setFitHeight(targetHeight);
        return imageView.snapshot(null, null);
    }
    

    此方法现在可用于 getImageFile()

        // 1 Scale image to avoid memory issues
        Image scaledImage = scaleImage(image);
    
        // 2. Encode image to png
        PngEncoderFX encoder = new PngEncoderFX(scaledImage, true);
        byte[] bytes = encoder.pngEncode();
    
        // 3. Write byte array to a file in public storage 
        ...
    

    这篇关于使用Gluon ShareService共享多个文件(image和txt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆