pickImage method

Future<XFile?> pickImage(
  1. {required ImageSource source,
  2. double? maxWidth,
  3. double? maxHeight,
  4. int? imageQuality,
  5. CameraDevice preferredCameraDevice = CameraDevice.rear,
  6. bool requestFullMetadata = true}
)

Returns an XFile object wrapping the image that was picked.

The returned XFile is intended to be used within a single app session. Do not save the file path and use it across sessions.

The source argument controls where the image comes from. This can be either ImageSource.camera or ImageSource.gallery.

Where iOS supports HEIC images, Android 8 and below doesn't. Android 9 and above only support HEIC images if used in addition to a size modification, of which the usage is explained below.

If specified, the image will be at most maxWidth wide and maxHeight tall. Otherwise the image will be returned at it's original width and height. The imageQuality argument modifies the quality of the image, ranging from 0-100 where 100 is the original/max quality. If imageQuality is null, the image with the original quality will be returned. Compression is only supported for certain image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked, a warning message will be logged.

Use preferredCameraDevice to specify the camera to use when the source is ImageSource.camera. The preferredCameraDevice is ignored when source is ImageSource.gallery. It is also ignored if the chosen camera is not supported on the device. Defaults to CameraDevice.rear. Note that Android has no documented parameter for an intent to specify if the front or rear camera should be opened, this function is not guaranteed to work on an Android device.

Use requestFullMetadata (defaults to true) to control how much additional information the plugin tries to get. If requestFullMetadata is set to true, the plugin tries to get the full image metadata which may require extra permission requests on some platforms, such as Photo Library Usage permission on iOS.

In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost in this call. You can then call retrieveLostData when your app relaunches to retrieve the lost data.

See also pickMultiImage to allow users to select multiple images at once.

The method could throw PlatformException if the app does not have permission to access the camera or photos gallery, no camera is available, plugin is already in use, temporary file could not be created (iOS only), plugin activity could not be allocated (Android only) or due to an unknown error.

Implementation

Future<XFile?> pickImage({
  required ImageSource source,
  double? maxWidth,
  double? maxHeight,
  int? imageQuality,
  CameraDevice preferredCameraDevice = CameraDevice.rear,
  bool requestFullMetadata = true,
}) {
  final ImagePickerOptions imagePickerOptions =
      ImagePickerOptions.createAndValidate(
    maxWidth: maxWidth,
    maxHeight: maxHeight,
    imageQuality: imageQuality,
    preferredCameraDevice: preferredCameraDevice,
    requestFullMetadata: requestFullMetadata,
  );

  return platform.getImageFromSource(
    source: source,
    options: imagePickerOptions,
  );
}