0

I have tried to use the function

SwingFXUtils.fromFXImage

which raises NoClassFoundError exception. How can i save an image otherwise on Gluon mobile?

tonimaroni
  • 1,062
  • 10
  • 19
  • What is your use case? Where does that image comes from? – José Pereda Feb 03 '17 at 21:01
  • I have used the PicturesService. ImageView imageView = new ImageView(); Services.get(PicturesService.class).ifPresent(service -> { service.takePhoto(false).ifPresent(image -> imageView.setImage(image)); }); So i try to use fromFXImage from this image. – tonimaroni Feb 03 '17 at 21:15
  • Ok, PicturesService returns an image (either from the camera or from the gallery), and you want to save it into a private or public storage location? – José Pereda Feb 03 '17 at 21:16
  • Yes, correctly. – tonimaroni Feb 03 '17 at 21:19

1 Answers1

1

SwingFXUtils nor any Swing related classes are supported on Android.

Based on your comments, you are using Charm Down PicturesService to retrieve an image from the camera and show it on an ImageView control:

Services.get(PicturesService.class).ifPresent(service -> 
    service.takePhoto(false).ifPresent(imageView::setImage));

And now you want to save that image into a private/public storage location on your device.

If you check the API for takePhoto, it has a savePhoto argument, that you can use to save the picture:

// take photo and save picture
Services.get(PicturesService.class).ifPresent(service -> 
    service.takePhoto(true).ifPresent(imageView::setImage));

Now if you have a look at how this is implemented, you will find your pic under the external storage for pictures:

File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "IMG_"+ timeStamp + ".jpg");

You can access that folder easily under /sdcard/Pictures.

Also you can use StorageService and getPublicStorage("Pictures"), and going through the directory you can retrieve the last file added:

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());
}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Perfect... Thank you for helping me out once again!! – tonimaroni Feb 03 '17 at 21:48
  • I would like to add one question... you can save pictures by telling takePhoto (true). The really big problem is... what is the filename of the saved picture. There might be dozens of others in the folder. Under android the filename contains the timestamp, but since I do not know the exact timestamp when the picture was taken, I only can checkout all existing pictures in the folder and try to figure out the picture that is closest to LocalDateTime.now()? Please do not tell me that this is what I have to do. – magicroomy Mar 14 '17 at 08:28
  • You can just sort files by last modification and get the first/last one? I don't think this is that bad? – José Pereda Mar 14 '17 at 10:08
  • Well, I would not want to reply on that procedure. Who says that there is no service or whatever running in parallel also creating files in that folder at the same time (multithreaded)? What if it happens between me taking the picture and afterwards trying to find the most recent file? I would get the wrong one. Maybe not too likely but possible. – magicroomy Mar 15 '17 at 12:03
  • The current PicturesService API main purpose is retrieving the (JavaFX) image and optionally you can save the file. If you have other needs, you are more than welcome to extend it and contribute to the open source [project](https://bitbucket.org/gluon-oss/charm-down/src/tip/plugins/plugin-pictures/android/src/main/java/com/gluonhq/charm/down/plugins/android/AndroidPicturesService.java?at=default&fileviewer=file-view-default#AndroidPicturesService.java-73). – José Pereda Mar 15 '17 at 12:08
  • In this case I could need some kickstart of how to compile it after having it cloned. Is there a doc how to compile it? gradlew distZip produces compile errors in Android although I set the ANDROID_HOME to my sdk dir in gradle.properties. And I did not even get to the IOS natives. – magicroomy Mar 16 '17 at 13:28
  • Usually I do `./gradlew clean install` on the root project of Charm Down, that will install it on my local repository. For testing you can use then `mavenLocal()` on repositories, and use the snapshot version for the plugins: `downConfig { version 3.3.0-SNAPSHOT ... }`. This should work on all platforms – José Pereda Mar 17 '17 at 09:02
  • But if you have any questions, post a new one, so we don't go through comments. – José Pereda Mar 17 '17 at 09:04