Java 与Gluon ShareService共享多个文件(图像和txt)
我们想知道如何与Gluon ShareService共享多个文件(图像和txt文件)。特别是如何与PictureService共享先前拍摄并存储(在图库中)的图像 但是我们需要先创建一个带有路径和图像名的文件。不幸的是,PictureService保存图像时,图像标题包含拍摄照片时的日期和时间 我们尝试使用loadImageFromGallery方法获取图像名称,但这会返回void并打开最近的屏幕 下面是我们试图分享的图像:Java 与Gluon ShareService共享多个文件(图像和txt),java,javafx,gluon,gluon-mobile,Java,Javafx,Gluon,Gluon Mobile,我们想知道如何与Gluon ShareService共享多个文件(图像和txt文件)。特别是如何与PictureService共享先前拍摄并存储(在图库中)的图像 但是我们需要先创建一个带有路径和图像名的文件。不幸的是,PictureService保存图像时,图像标题包含拍摄照片时的日期和时间 我们尝试使用loadImageFromGallery方法获取图像名称,但这会返回void并打开最近的屏幕 下面是我们试图分享的图像: public void sharePicture() { Se
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);
});
});
}
- 我们如何用我们想要的标题将图像存储到我们想要的位置李>
- 我们如何一起共享文件和图像
图像
转换为文件
到目前为止,只返回一个JavaFX图像,而不是一个文件,因此我们需要一种方法将该图像保存到一个我们可以读取和共享的文件中
而且这个过程并不容易,因为在手机上我们没有swinggutilities
最初使用PixelReader
读取图像并获取字节数组的方法并不奏效,因为它会给你一个无法读取或共享的大原始文件
我使用了一个PNG编码器从JavaFX图像中获取PNG的字节数组:
PngEncoderFX encoder = new PngEncoderFX(image, true);
byte[] bytes = encoder.pngEncode();
然后,我将该字节数组保存到公共存储文件夹中的一个文件中(以便共享),我可以使用`存储服务:
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;
}
现在,您可以调用PictureService
,检索图像,将其保存到文件中,最后执行以下操作:
请注意,如果您尝试对大图像进行编码,可能会遇到内存问题
无论如何,如果PictureService首先返回一个文件,那么所有的过程都可以简化。如果您想提交问题,您可以这样做
编辑
避免内存问题和减小共享文件大小的一个可能解决方案是,如果原始图像超过一定大小,则缩小原始图像,就像PictureService的iOS实现中已经完成的那样:
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()
中使用此方法:
非常感谢你!存储和共享图像现在可以正常工作了。但我们仍然难以同时共享多个文件(txt和jpg文件)。你也有解决办法吗?拉链也可以。谢谢你的帮助!:)PictureService一次只允许一个选择。考虑到这一点,您可以分两步完成:首先,选择图像并将其存储在公共存储文件夹中,但不进行共享。然后,当用户选择了所有图像后,使用共享按钮。既然你有了这些图片,你就可以和他们做一个交流,并最终分享。再次感谢你的回答!我们决定使用一个zip文件来共享所有图片亲爱的José,您是否知道您建议的PngEncoderFX实现是否用于新的Gluon版本5?:-)Gluon Mobile不使用它,但请检查Charm Down 3.8.0中的PictureService,它包含新的API。
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);
}
// 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
...