-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[image_picker] Fix android-16 not picking up file #11320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a0f3b70
362cf17
afecdf1
f70492c
385d2e3
da290e7
b1a240d
ce77b61
748ba3b
681bd67
7194411
76ed499
256b9f8
ab51db2
8eceb37
0d2e2bf
791842d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,19 @@ should add it to your `pubspec.yaml` as usual. | |
|
|
||
| ## Photo Picker | ||
|
|
||
| On Android 13 and above this packages uses the Android Photo Picker. | ||
| On **Android 16** (API level 36) and above, gallery image, video, and mixed-media | ||
| picks always use the Android Photo Picker. [`ImagePickerAndroid.useAndroidPhotoPicker`][4] | ||
| cannot be set to `false` to use the legacy `ACTION_GET_CONTENT` flow on those | ||
| versions; it would often return no path. See [flutter/flutter#182071][5]. | ||
|
|
||
| On Android 12 and below this package has optional Android Photo Picker functionality. | ||
| On Android 13 through 15, the default is to use the legacy picker; you can opt in to | ||
| the Photo Picker with the flag below. | ||
|
|
||
| To use this feature, add the following code to your app before calling any `image_picker` APIs: | ||
| On Android 12 and below this package has optional Android Photo Picker functionality | ||
| (when the flag is `true` and the system supports it). | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me what distinction is being made between <=12 and 13-15. Don't they have the same behavior of being opt-in? (IIRC the distinction in the old text between 13+ and <13 is incorrect, and dates to before we made it optional everywhere due to limitations.) |
||
|
|
||
| To opt in on versions where it is optional, add the following code to your app before | ||
| calling any `image_picker` APIs: | ||
|
|
||
| <?code-excerpt "main.dart (photo-picker-example)"?> | ||
| ```dart | ||
|
|
@@ -33,8 +41,13 @@ import 'package:image_picker_platform_interface/image_picker_platform_interface. | |
| } | ||
| ``` | ||
|
|
||
| In addition, `ImagePickerAndroid.useAndroidPhotoPicker` must be set to `true` to use the `limit` functionality. It is implemented based on [`ActivityResultContract`][3], so it can only be ensured to take effect on Android 13 or above. Otherwise, it depends on whether the corresponding system app supports it. | ||
| In addition, `ImagePickerAndroid.useAndroidPhotoPicker` must be set to `true` to use | ||
| the `limit` functionality. It is implemented based on [`ActivityResultContract`][3], so | ||
| it can only be ensured to take effect on Android 13 or above. Otherwise, it depends on | ||
| whether the corresponding system app supports it. | ||
|
|
||
| [1]: https://pub.dev/packages/image_picker | ||
| [2]: https://flutter.dev/to/endorsed-federated-plugin | ||
| [3]: https://developer.android.google.cn/reference/kotlin/androidx/activity/result/contract/ActivityResultContracts.PickMultipleVisualMedia | ||
| [4]: https://pub.dev/documentation/image_picker_android/latest/image_picker_android/ImagePickerAndroid/useAndroidPhotoPicker.html | ||
| [5]: https://github.com/flutter/flutter/issues/182071 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.imagepicker; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.annotation.Config; | ||
|
|
||
| @RunWith(RobolectricTestRunner.class) | ||
| public class ImagePickerUtilsTest { | ||
|
|
||
| @Test | ||
| @Config(sdk = 35) | ||
| public void effectiveUsePhotoPicker_belowApi36_usesDartPreference() { | ||
| assertFalse(ImagePickerUtils.effectiveUsePhotoPicker(false)); | ||
| assertTrue(ImagePickerUtils.effectiveUsePhotoPicker(true)); | ||
| } | ||
|
|
||
| @Test | ||
| @Config(sdk = 36) | ||
| public void effectiveUsePhotoPicker_onApi36_alwaysUsesPhotoPicker() { | ||
| assertTrue(ImagePickerUtils.effectiveUsePhotoPicker(false)); | ||
| assertTrue(ImagePickerUtils.effectiveUsePhotoPicker(true)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,9 +17,21 @@ class ImagePickerAndroid extends ImagePickerPlatform { | |
|
|
||
| final ImagePickerApi _hostApi; | ||
|
|
||
| /// Sets [ImagePickerAndroid] to use Android 13 Photo Picker. | ||
| /// Whether gallery picks use the Android Photo Picker instead of the legacy | ||
| /// document picker (`ACTION_GET_CONTENT`). | ||
| /// | ||
| /// Currently defaults to false, but the default is subject to change. | ||
| /// Defaults to `false` (the default may change in a future release). When `true`, | ||
| /// the plugin uses the Photo Picker on Android versions where it is available for | ||
| /// the requested operation. | ||
| /// | ||
| /// **Android 16** (API level 36) and higher: the plugin always uses the Photo Picker | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is critical information that should be summarized in the first sentence of the docs. I would expect something like: |
||
| /// for gallery image, video, and mixed-media selection. This field is **not** | ||
| /// respected for opting out on those versions, because the legacy flow can return | ||
| /// success without a usable file URI. See | ||
| /// https://github.com/flutter/flutter/issues/182071 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User-facing docs don't need to explain the history of the change, just the behavior. |
||
| /// | ||
| /// Must be `true` to use the multi-select `limit` parameter on Android 13 and | ||
| /// above, where that feature is implemented via the Photo Picker. | ||
| bool useAndroidPhotoPicker = false; | ||
|
|
||
| /// Registers this class as the default platform implementation. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have evidence that it "often" returns no path, but as with the docs we don't need to justify this in the README, just explain it.