-
Couldn't load subscription status.
- Fork 3.5k
[image_picker] Fix/152156 non image files picked #10238
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?
[image_picker] Fix/152156 non image files picked #10238
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
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.
Code Review
This pull request correctly fixes an issue on Android where ImagePicker.pickMedia would allow selecting files of any type. The change replaces a non-standard intent extra key with the correct Intent.EXTRA_MIME_TYPES. The fix is correct and the related CHANGELOG.md and pubspec.yaml updates are appropriate. I've added one comment suggesting the addition of a unit test to prevent future regressions of this behavior, in line with the repository's testing guidelines.
| pickMediaIntent.setType("*/*"); | ||
| String[] mimeTypes = {"video/*", "image/*"}; | ||
| pickMediaIntent.putExtra("CONTENT_TYPE", mimeTypes); | ||
| pickMediaIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes); |
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.
This change correctly fixes the issue by using the standard Intent.EXTRA_MIME_TYPES.
To ensure this functionality is covered against future regressions, please consider adding a unit test.1 A test could verify that when usePhotoPicker is false, the launched Intent has the correct action, type, and EXTRA_MIME_TYPES set.
Here is an example of what such a test could look like in ImagePickerDelegateTest.java:
@Test
@Config(sdk = 30) // To test the 'else' branch for non-photo-picker
public void chooseMediaFromGallery_launchesIntentWithCorrectMimeTypes() {
ImagePickerDelegate delegate = createDelegate();
GeneralOptions generalOptions =
new GeneralOptions.Builder().setAllowMultiple(false).setUsePhotoPicker(false).build();
delegate.chooseMediaFromGallery(DEFAULT_MEDIA_OPTIONS, generalOptions, mockResult);
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mockActivity)
.startActivityForResult(
intentCaptor.capture(), eq(ImagePickerDelegate.REQUEST_CODE_CHOOSE_MEDIA_FROM_GALLERY));
Intent intent = intentCaptor.getValue();
assertEquals(Intent.ACTION_GET_CONTENT, intent.getAction());
assertEquals("*/*", intent.getType());
String[] mimeTypes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
assertArrayEquals(new String[]{"video/*", "image/*"}, mimeTypes);
}Style Guide References
Footnotes
|
Thanks for the contribution!
The previous implementation also attempted to do this; do you have links to relevant docs explaining that this approach is correct, or other information about how you tested this? Given that different devices will have different default handlers, and users can add non-default handlers, it may well be the case that some handlers look at one of these keys and others look at the other, for example.
This PR will need tests before it can be reviewed. Please mark as Ready for Review once the tests have been added. |
|
The official Android documentation mentions EXTRA_MIME_TYPES only in connection with ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT, not with ACTION_GET_CONTENT. https://developer.android.com/guide/components/intents-common#OpenFile But it is recognized by system pickers when used. In cases where a picker doesn’t honor this extra, the intent should fall back to the primary MIME type defined via setType(), as it currently does in production with the existing CONTENT_TYPE. Testing was done only on a few real devices, all of those supported this picker version. It may be important to add that, with these changes, the Android Photo picker is shown by default, with an additional option to browse all files, where only media is allowed. |
This is just an assertion of the implicit claim of the PR, which is that "system pickers" (all of them?) do recognize Imagine for the sake of argument that the reality actually that of the two most common OEM default pickers, one recognizes only In order to avoid that, we need to understand (and record) what the basis of the claims in PRs is, rather than just accept them without evidence.
Yes, but that doesn't actually have the desired behavior, so that's not a good fallback.
If you only tested one specific picker, how do you know that there are not production devices that use |
|
Now I understand. All of these assumptions came from tests I ran on real devices and emulators — Pixel 7 (Android 14), Pixel 8 Pro (Android 16), and Samsung Galaxy A12 (Android 12) — and from the fact that I couldn’t find any official Android documentation mentioning CONTENT_TYPE. I’m afraid I don’t have any additional data or evidence that can guarantee this works on all pickers beyond those tests. I’m open to suggestions on how to handle this more reliably or to broaden testing if needed. Maybe the safest approach would be to support both EXTRA_MIME_TYPES and CONTENT_TYPE. On my test devices, providing both produced the expected behavior. |
[image_picker]fix(android): prevent non-image files from being picked in pickMedia (Fixes #152156)This PR fixes an issue where
ImagePicker.pickMediaon Android allowed users to select non-media files, even when the intent should have restricted the selection to image and video MIME types.What this PR changes
pickMediato explicitly limit the intent’s MIME types toimage/*andvideo/*.Why this change is needed
Prior to this fix, users could inadvertently pick files outside the intended scope (e.g., PDFs ).
Before / After
Before:
pickMedia.After:
Fixes #152156 - flutter/flutter#152156
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3