pickMedia method
Returns an XFile of the image or video that was picked.
The image or video can only come from the gallery.
The returned XFile is intended to be used within a single app session. Do not save the file path and use it across sessions.
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.
This method is not supported in iOS versions lower than 14.
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 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.
The method could throw PlatformException
if the app does not have permission to access
the photos gallery, 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.
If no image or video was picked, the return value is null.
Implementation
Future<XFile?> pickMedia({
double? maxWidth,
double? maxHeight,
int? imageQuality,
bool requestFullMetadata = true,
}) async {
final List<XFile> listMedia = await platform.getMedia(
options: MediaOptions.createAndValidate(
imageOptions: ImageOptions.createAndValidate(
maxHeight: maxHeight,
maxWidth: maxWidth,
imageQuality: imageQuality,
requestFullMetadata: requestFullMetadata,
),
allowMultiple: false,
),
);
return listMedia.isNotEmpty ? listMedia.first : null;
}