getPublicStorage(" Pictures")列出没有文件 [英] getPublicStorage("Pictures") lists no files

查看:176
本文介绍了getPublicStorage(" Pictures")列出没有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用

File picturesDir = Services.get(StorageService.class)
            .flatMap(s -> s.getPublicStorage("Pictures"))
            .orElseThrow(() -> new RuntimeException("Error retrieving public storage")); 
for (File pic : picturesDir.listFiles()) {
        System.out.println("file " + pic.getName());
}

列出没有文件。我认为它应该列出iPhone上我的图像库中的所有文件。

lists no files. I think it should list all files from my Image-Gallery on iPhone.

调用s.getPublicStorage()它列出了两个文件夹:
gluon,图片。

Calling s.getPublicStorage("") it lists two folders though: gluon, Pictures.

如何正确访问?

推荐答案

如前所述在这个问题中,在移动设备上没有Swing或AWT,所以一旦你有了一个JavaFX图像,检索它你不能使用 SwingFXUtils

As discussed previously in this question, on mobile there is no Swing or AWT, so once you have a JavaFX image, to retrieve it you can't use SwingFXUtils.

上述问题中提出的解决方案适用于Android,因为你可以轻松获取文件。相反,在iOS上,该文件位于图库中,当前的Charm Down PicturesService 无法访问图库。

The solution proposed in the mentioned question works on Android as you can easily get the File. On iOS, on the contrary, the file is located in the gallery and the current Charm Down PicturesService doesn't access the gallery.

而不是修改该服务(使用这样的本机代码 one ),从JavaFX Image 获取可以发送到Web服务的byteArray的想法基于两个步骤:

Instead of modifying that service (with native code like this one), the idea to get a byteArray from the JavaFX Image that you can send to a web service is based on two steps:


  • 将图像像素作为字节数组获取

  • 将字节数组编码为字符串。

如果您检查 PicturesService :: takePhoto 的iOS实现,图像从iOS本机层通过Base64发送到JavaFX层 encoding 解码

If you check the iOS implementation for PicturesService::takePhoto, the image from the iOS native layer is sent to the JavaFX layer via Base64 encoding and decoding.

解决方案1 ​​

此代码段适用于我:

// Take a photo and add image to imageView
Button button = new Button("Take Photo");
button.setOnAction(e ->
    Services.get(PicturesService.class)
        .ifPresent(s -> s.takePhoto(false).ifPresent(imageView::setImage)));

// Encode image
imageView.imageProperty().addListener((obs, ov, image) -> {
    if (image != null) {
        // 1. image to byte array
        PixelReader pixelReader = image.getPixelReader();
        int width = (int) image.getWidth(); 
        int height = (int) image.getHeight(); 
        byte[] buffer = new byte[width * height * 4]; 
        pixelReader.getPixels(0, 0, width, height, PixelFormat.getByteBgraInstance(), buffer, 0, width * 4); 

        // 2. Encode to String
        String encoded = Base64.getEncoder().encodeToString(buffer);

        // 3. send string...
    }
} 

您可以检查流程的工作方式反转这些步骤并从编码的字符串中创建一个图像:

You can check the process works by reversing these steps and creating an image out of the encoded string:

byte[] imageBytes = Base64.getDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8));

WritablePixelFormat<ByteBuffer> wf = PixelFormat.getByteBgraInstance();
WritableImage writableImage = new WritableImage(width, height);
PixelWriter pixelWriter = writableImage.getPixelWriter();
pixelWriter.setPixels(0, 0, width, height, wf, imageBytes, 0, width * 4);

imageView.setImage(writableImage);

解决方案2

如果要将字节数组从 PixelReader :: getPixels 转换为 BufferedImage ,则不会work:字节数组不同。

If you want to transform the byte array from PixelReader::getPixels into a BufferedImage, that won't work: the byte arrays are different.

所以你需要使用缓冲图像能够处理的东西。

So you need to use something that the buffered image will be able to process.

看一下 SwingFXUtils 实现,它改为使用int数组。

Having a look at the SwingFXUtils implementation, it uses an int array instead.

所以这是另一种可能性:

So this is another possibility:

// Take a photo and add image to imageView
Button button = new Button("Take Photo");
button.setOnAction(e ->
    Services.get(PicturesService.class)
        .ifPresent(s -> s.takePhoto(false).ifPresent(imageView::setImage)));

// Encode image
imageView.imageProperty().addListener((obs, ov, image) -> {
    if (image != null) {
        // 1. image to int array
        PixelReader pixelReader = image.getPixelReader();
        int width = (int) image.getWidth(); 
        int height = (int) image.getHeight(); 
        int[] data = new int[width * height]; 
        pixelReader.getPixels(0, 0, width, height, PixelFormat.getIntArgbPreInstance(), data, 0, width); 

        // 2. int array to byte array
        ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);        
        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(data);


        // 3. Encode to String
        String encoded = Base64.getEncoder().encodeToString(byteBuffer.array());

        // 4. send string...
    }
} 

现在你必须解码字符串,得到int arr ay并创建缓冲图像:

Now you will have to decode the string, get the int array and create the buffered image:

// 1. Decode string
byte[] imageBytes = Base64.getDecoder().decode(encoded.getBytes(StandardCharsets.UTF_8));

// 2. get int array
ByteBuffer byteBuffer2 = ByteBuffer.wrap(imageBytes);
IntBuffer intBuffer2 = byteBuffer2.asIntBuffer();
int[] imageData = new int[intBuffer2.limit()];
intBuffer2.get(imageData);

// 3. create buffered image
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
bufferedImage.setRGB(0, 0, width, height, imageData, 0, width);

这篇关于getPublicStorage(&quot; Pictures&quot;)列出没有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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