image_picker.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2013 The Flutter Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // ignore_for_file: deprecated_member_use, deprecated_member_use_from_same_package
  5. import 'dart:async';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
  8. export 'package:image_picker_platform_interface/image_picker_platform_interface.dart'
  9. show
  10. kTypeImage,
  11. kTypeVideo,
  12. ImageSource,
  13. CameraDevice,
  14. LostData,
  15. PickedFile,
  16. RetrieveType;
  17. /// Provides an easy way to pick an image/video from the image library,
  18. /// or to take a picture/video with the camera.
  19. class ImagePicker {
  20. /// The platform interface that drives this plugin
  21. @visibleForTesting
  22. static ImagePickerPlatform get platform => ImagePickerPlatform.instance;
  23. /// Returns a [PickedFile] object wrapping the image that was picked.
  24. ///
  25. /// The returned [PickedFile] is intended to be used within a single APP session. Do not save the file path and use it across sessions.
  26. ///
  27. /// The `source` argument controls where the image comes from. This can
  28. /// be either [ImageSource.camera] or [ImageSource.gallery].
  29. ///
  30. /// Where iOS supports HEIC images, Android 8 and below doesn't. Android 9 and above only support HEIC images if used
  31. /// in addition to a size modification, of which the usage is explained below.
  32. ///
  33. /// If specified, the image will be at most `maxWidth` wide and
  34. /// `maxHeight` tall. Otherwise the image will be returned at it's
  35. /// original width and height.
  36. /// The `imageQuality` argument modifies the quality of the image, ranging from 0-100
  37. /// where 100 is the original/max quality. If `imageQuality` is null, the image with
  38. /// the original quality will be returned. Compression is only supported for certain
  39. /// image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked,
  40. /// a warning message will be logged.
  41. ///
  42. /// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
  43. /// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
  44. /// Defaults to [CameraDevice.rear]. Note that Android has no documented parameter for an intent to specify if
  45. /// the front or rear camera should be opened, this function is not guaranteed
  46. /// to work on an Android device.
  47. ///
  48. /// In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost
  49. /// in this call. You can then call [getLostData] when your app relaunches to retrieve the lost data.
  50. Future<PickedFile?> getImage({
  51. required ImageSource source,
  52. double? maxWidth,
  53. double? maxHeight,
  54. int? imageQuality,
  55. CameraDevice preferredCameraDevice = CameraDevice.rear,
  56. }) {
  57. return platform.pickImage(
  58. source: source,
  59. maxWidth: maxWidth,
  60. maxHeight: maxHeight,
  61. imageQuality: imageQuality,
  62. preferredCameraDevice: preferredCameraDevice,
  63. );
  64. }
  65. /// Returns a [PickedFile] object wrapping the video that was picked.
  66. ///
  67. /// The returned [PickedFile] is intended to be used within a single APP session. Do not save the file path and use it across sessions.
  68. ///
  69. /// The [source] argument controls where the video comes from. This can
  70. /// be either [ImageSource.camera] or [ImageSource.gallery].
  71. ///
  72. /// The [maxDuration] argument specifies the maximum duration of the captured video. If no [maxDuration] is specified,
  73. /// the maximum duration will be infinite.
  74. ///
  75. /// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
  76. /// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
  77. /// Defaults to [CameraDevice.rear].
  78. ///
  79. /// In Android, the MainActivity can be destroyed for various fo reasons. If that happens, the result will be lost
  80. /// in this call. You can then call [getLostData] when your app relaunches to retrieve the lost data.
  81. Future<PickedFile?> getVideo({
  82. required ImageSource source,
  83. CameraDevice preferredCameraDevice = CameraDevice.rear,
  84. Duration? maxDuration,
  85. }) {
  86. return platform.pickVideo(
  87. source: source,
  88. preferredCameraDevice: preferredCameraDevice,
  89. maxDuration: maxDuration,
  90. );
  91. }
  92. /// Retrieve the lost [PickedFile] when [selectImage] or [selectVideo] failed because the MainActivity is destroyed. (Android only)
  93. ///
  94. /// Image or video can be lost if the MainActivity is destroyed. And there is no guarantee that the MainActivity is always alive.
  95. /// Call this method to retrieve the lost data and process the data according to your APP's business logic.
  96. ///
  97. /// Returns a [LostData] object if successfully retrieved the lost data. The [LostData] object can represent either a
  98. /// successful image/video selection, or a failure.
  99. ///
  100. /// Calling this on a non-Android platform will throw [UnimplementedError] exception.
  101. ///
  102. /// See also:
  103. /// * [LostData], for what's included in the response.
  104. /// * [Android Activity Lifecycle](https://developer.android.com/reference/android/app/Activity.html), for more information on MainActivity destruction.
  105. Future<LostData> getLostData() {
  106. return platform.retrieveLostData();
  107. }
  108. }