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.imageProperty().addListener((obs, ov, image) -> {
          if (image != null) {
              imageView.setImage(image);
          }
        });
        service.asyncTakePhoto(false);
      });

    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.imageProperty().addListener((obs, ov, image) -> {
              if (image != null) {
                  imageView.setImage(image);
                  service.getImageFile().ifPresent(file ->
                      ShareService.create().ifPresent(share ->
                          share.share("image/jpeg", file)));
              }
          });
          service.asyncLoadImageFromGallery();
      });

    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

        @Deprecated
        java.util.Optional<javafx.scene.image.Image> takePhoto​(boolean savePhoto)
        Deprecated.
        This method has been deprecated in favour of asyncTakePhoto(boolean).
        Use the device's camera to take a photo, in a blocking way, 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
      • asyncTakePhoto

        void asyncTakePhoto​(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
        Since:
        4.0.16
      • loadImageFromGallery

        @Deprecated
        java.util.Optional<javafx.scene.image.Image> loadImageFromGallery()
        Deprecated.
        This method has been deprecated in favour of asyncLoadImageFromGallery().
        Retrieve an image from the device's gallery of images, in a blocking way.
        Returns:
        an Optional with the Image or empty if it failed, or it was cancelled
      • asyncLoadImageFromGallery

        void asyncLoadImageFromGallery()
        Retrieve an image from the device's gallery of images.
        Since:
        4.0.16
      • 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
      • imageProperty

        javafx.beans.property.ReadOnlyObjectProperty<javafx.scene.image.Image> imageProperty()
        A read-only property containing the image taken from the camera or gallery or null if it failed, or it was cancelled.
        Returns:
        a read-only object property containing an image that can be null
        Since:
        4.0.16