Interface PicturesService

  • All Known Implementing Classes:
    AndroidPicturesService, DummyPicturesService, IOSPicturesService

    public interface PicturesService
    The picture service allows the developer to load a picture from the device's local file system or from a picture taken directly using the device's camera.

    Example

     ImageView imageView = new ImageView();
      PicturesService.create().ifPresent(service -> {
          service.takePhoto(false).ifPresent(image -> imageView.setImage(image));
      });

    It also allows the developer to retrieve the original file and work with it as needed, for instance sharing it with the ShareService.

    Example

     ImageView imageView = new ImageView();
      PicturesService.create().ifPresent(service -> {
          service.loadImageFromGallery().ifPresent(image -> imageView.setImage(image));
          service.getImageFile().ifPresent(file ->
              ShareService.create().ifPresent(share ->
                  share.share("image/jpeg", file)));
      });

    Requirements

    The service requires the following changes on Android and iOS.

    However, these are handled automatically by the GluonFX plugin, when used.

    Android Configuration

    Create the file /src/android/res/xml/file_provider_paths.xml with the following content that allows access to the external storage:

     
        <?xml version="1.0" encoding="utf-8"?>
        <paths>
            <external-path name="external_files" path="." />
        </paths>
     
     

    The permission android.permission.CAMERA needs to be added as well as the permissions android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE to be able to read and write images. Also a provider is required:

     <manifest package="${application.package.name}" ...>
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <application ...>
           ...
           <activity android:name="com.gluonhq.attach.android.PermissionRequestActivity" />
           <provider
               android:name="android.support.v4.content.FileProvider"
               android:authorities="${application.package.name}.fileprovider"
               android:exported="false"
               android:grantUriPermissions="true">
               <meta-data
                   android:name="android.support.FILE_PROVIDER_PATHS"
                   android:resource="@xml/file_provider_paths" />
           </provider>
       </application>
     </manifest>
     

    iOS Configuration

    The following keys are required:

     <key>NSCameraUsageDescription</key>
      <string>Reason to use Camera Service (iOS 10+)</string>
      <key>NSPhotoLibraryUsageDescription</key>
      <string>Reason to use Photo Library (iOS 10+)</string>
      <key>NSPhotoLibraryAddUsageDescription</key>
      <string>Reason to use Photo Library (iOS 10+)</string>
    Since:
    3.0.0
    • Method Detail

      • takePhoto

        java.util.Optional<javafx.scene.image.Image> takePhoto​(boolean savePhoto)
        Use the device's camera to take a photo, and retrieve an Image. It can be saved as well in the device's public album.
        Parameters:
        savePhoto - if true, image is saved to public album
        Returns:
        an Optional with the Image or empty if it failed or it was cancelled
      • loadImageFromGallery

        java.util.Optional<javafx.scene.image.Image> loadImageFromGallery()
        Retrieve an image from the device's gallery of images
        Returns:
        an Optional with the Image or empty if it failed or it was cancelled
      • getImageFile

        java.util.Optional<java.io.File> getImageFile()
        Retrieve the file associated to the original picture generated by takePhoto(true) or the file related to the picture selected with loadImageFromGallery().
        Returns:
        an Optional with the File associated with the original image.
        Since:
        3.8.0