image_picker.dart 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2019 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 'dart:io';
  7. import 'package:flutter/foundation.dart';
  8. import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
  9. export 'package:image_picker_platform_interface/image_picker_platform_interface.dart'
  10. show
  11. kTypeImage,
  12. kTypeVideo,
  13. ImageSource,
  14. CameraDevice,
  15. LostData,
  16. LostDataResponse,
  17. PickedFile,
  18. RetrieveType;
  19. /// Provides an easy way to pick an image/video from the image library,
  20. /// or to take a picture/video with the camera.
  21. class ImagePicker {
  22. /// The platform interface that drives this plugin
  23. @visibleForTesting
  24. static ImagePickerPlatform platform = ImagePickerPlatform.instance;
  25. /// Returns a [File] object pointing to the image that was picked.
  26. ///
  27. /// The returned [File] is intended to be used within a single APP session. Do not save the file path and use it across sessions.
  28. ///
  29. /// The `source` argument controls where the image comes from. This can
  30. /// be either [ImageSource.camera] or [ImageSource.gallery].
  31. ///
  32. /// If specified, the image will be at most `maxWidth` wide and
  33. /// `maxHeight` tall. Otherwise the image will be returned at it's
  34. /// original width and height.
  35. /// The `imageQuality` argument modifies the quality of the image, ranging from 0-100
  36. /// where 100 is the original/max quality. If `imageQuality` is null, the image with
  37. /// the original quality will be returned. Compression is only supportted for certain
  38. /// image types such as JPEG. If compression is not supported for the image that is picked,
  39. /// an warning message will be logged.
  40. ///
  41. /// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
  42. /// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
  43. /// Defaults to [CameraDevice.rear].
  44. ///
  45. /// In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost
  46. /// in this call. You can then call [retrieveLostData] when your app relaunches to retrieve the lost data.
  47. @Deprecated('Use imagePicker.getImage() method instead.')
  48. static Future<File> pickImage(
  49. {@required ImageSource source,
  50. double maxWidth,
  51. double maxHeight,
  52. int imageQuality,
  53. CameraDevice preferredCameraDevice = CameraDevice.rear}) async {
  54. String path = await platform.pickImagePath(
  55. source: source,
  56. maxWidth: maxWidth,
  57. maxHeight: maxHeight,
  58. imageQuality: imageQuality,
  59. preferredCameraDevice: preferredCameraDevice,
  60. );
  61. return path == null ? null : File(path);
  62. }
  63. /// Returns a [PickedFile] object wrapping the image that was picked.
  64. ///
  65. /// The returned [PickedFile] is intended to be used within a single APP session. Do not save the file path and use it across sessions.
  66. ///
  67. /// The `source` argument controls where the image comes from. This can
  68. /// be either [ImageSource.camera] or [ImageSource.gallery].
  69. ///
  70. /// If specified, the image will be at most `maxWidth` wide and
  71. /// `maxHeight` tall. Otherwise the image will be returned at it's
  72. /// original width and height.
  73. /// The `imageQuality` argument modifies the quality of the image, ranging from 0-100
  74. /// where 100 is the original/max quality. If `imageQuality` is null, the image with
  75. /// the original quality will be returned. Compression is only supportted for certain
  76. /// image types such as JPEG. If compression is not supported for the image that is picked,
  77. /// an warning message will be logged.
  78. ///
  79. /// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
  80. /// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
  81. /// Defaults to [CameraDevice.rear].
  82. ///
  83. /// In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost
  84. /// in this call. You can then call [getLostData] when your app relaunches to retrieve the lost data.
  85. Future<PickedFile> getImage({
  86. @required ImageSource source,
  87. double maxWidth,
  88. double maxHeight,
  89. int imageQuality,
  90. CameraDevice preferredCameraDevice = CameraDevice.rear,
  91. }) {
  92. return platform.pickImage(
  93. source: source,
  94. maxWidth: maxWidth,
  95. maxHeight: maxHeight,
  96. imageQuality: imageQuality,
  97. preferredCameraDevice: preferredCameraDevice,
  98. );
  99. }
  100. /// Returns a [File] object pointing to the video that was picked.
  101. ///
  102. /// The returned [File] is intended to be used within a single APP session. Do not save the file path and use it across sessions.
  103. ///
  104. /// The [source] argument controls where the video comes from. This can
  105. /// be either [ImageSource.camera] or [ImageSource.gallery].
  106. ///
  107. /// The [maxDuration] argument specifies the maximum duration of the captured video. If no [maxDuration] is specified,
  108. /// the maximum duration will be infinite.
  109. ///
  110. /// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
  111. /// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
  112. /// Defaults to [CameraDevice.rear].
  113. ///
  114. /// In Android, the MainActivity can be destroyed for various fo reasons. If that happens, the result will be lost
  115. /// in this call. You can then call [retrieveLostData] when your app relaunches to retrieve the lost data.
  116. @Deprecated('Use imagePicker.getVideo() method instead.')
  117. static Future<File> pickVideo(
  118. {@required ImageSource source,
  119. CameraDevice preferredCameraDevice = CameraDevice.rear,
  120. Duration maxDuration}) async {
  121. String path = await platform.pickVideoPath(
  122. source: source,
  123. preferredCameraDevice: preferredCameraDevice,
  124. maxDuration: maxDuration,
  125. );
  126. return path == null ? null : File(path);
  127. }
  128. /// Returns a [PickedFile] object wrapping the video that was picked.
  129. ///
  130. /// The returned [PickedFile] is intended to be used within a single APP session. Do not save the file path and use it across sessions.
  131. ///
  132. /// The [source] argument controls where the video comes from. This can
  133. /// be either [ImageSource.camera] or [ImageSource.gallery].
  134. ///
  135. /// The [maxDuration] argument specifies the maximum duration of the captured video. If no [maxDuration] is specified,
  136. /// the maximum duration will be infinite.
  137. ///
  138. /// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
  139. /// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
  140. /// Defaults to [CameraDevice.rear].
  141. ///
  142. /// In Android, the MainActivity can be destroyed for various fo reasons. If that happens, the result will be lost
  143. /// in this call. You can then call [getLostData] when your app relaunches to retrieve the lost data.
  144. Future<PickedFile> getVideo({
  145. @required ImageSource source,
  146. CameraDevice preferredCameraDevice = CameraDevice.rear,
  147. Duration maxDuration,
  148. }) {
  149. return platform.pickVideo(
  150. source: source,
  151. preferredCameraDevice: preferredCameraDevice,
  152. maxDuration: maxDuration,
  153. );
  154. }
  155. /// Retrieve the lost image file when [pickImage] or [pickVideo] failed because the MainActivity is destroyed. (Android only)
  156. ///
  157. /// Image or video can be lost if the MainActivity is destroyed. And there is no guarantee that the MainActivity is always alive.
  158. /// Call this method to retrieve the lost data and process the data according to your APP's business logic.
  159. ///
  160. /// Returns a [LostDataResponse] if successfully retrieved the lost data. The [LostDataResponse] can represent either a
  161. /// successful image/video selection, or a failure.
  162. ///
  163. /// Calling this on a non-Android platform will throw [UnimplementedError] exception.
  164. ///
  165. /// See also:
  166. /// * [LostDataResponse], for what's included in the response.
  167. /// * [Android Activity Lifecycle](https://developer.android.com/reference/android/app/Activity.html), for more information on MainActivity destruction.
  168. static Future<LostDataResponse> retrieveLostData() {
  169. return platform.retrieveLostDataAsDartIoFile();
  170. }
  171. /// Retrieve the lost [PickedFile] when [selectImage] or [selectVideo] failed because the MainActivity is destroyed. (Android only)
  172. ///
  173. /// Image or video can be lost if the MainActivity is destroyed. And there is no guarantee that the MainActivity is always alive.
  174. /// Call this method to retrieve the lost data and process the data according to your APP's business logic.
  175. ///
  176. /// Returns a [LostData] object if successfully retrieved the lost data. The [LostData] object can represent either a
  177. /// successful image/video selection, or a failure.
  178. ///
  179. /// Calling this on a non-Android platform will throw [UnimplementedError] exception.
  180. ///
  181. /// See also:
  182. /// * [LostData], for what's included in the response.
  183. /// * [Android Activity Lifecycle](https://developer.android.com/reference/android/app/Activity.html), for more information on MainActivity destruction.
  184. Future<LostData> getLostData() {
  185. return platform.retrieveLostData();
  186. }
  187. }