Skip to content

[image_picker_android] Improved Bitmap resize on Android - #3423

Merged
auto-submit[bot] merged 19 commits into
flutter:mainfrom
beroso:118383-improve-android-bitmap-resize
May 10, 2023
Merged

auto-submit[bot] merged 19 commits into
flutter:mainfrom
beroso:118383-improve-android-bitmap-resize

Conversation

@beroso

@beroso beroso commented Mar 8, 2023 •

Copy link
Copy Markdown
Contributor

Improves Bitmap load and resize on Android.

Original PR on flutter/plugins: flutter-team-archive/plugins#6947

Issue: flutter/flutter#118383

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran [the auto-formatter]. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the [CLA].
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes].
  • I updated CHANGELOG.md to add a description of the change, [following repository CHANGELOG style].
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@gmackall

Copy link
Copy Markdown
Member

Re-requesting review from @tarrinneal and @stuartmorgan as you were both requested on the previous version of this PR.

@tarrinneal

Copy link
Copy Markdown
Contributor

Have you run any benchmarks to see if/how much this is better than the old behaviour?

Comment thread packages/image_picker/image_picker_android/CHANGELOG.md Outdated
@beroso
beroso requested review from stuartmorgan-g and removed request for gmackall and tarrinneal March 21, 2023 10:56
@beroso

beroso commented Mar 21, 2023

Copy link
Copy Markdown
Contributor Author

Sorry, guys. I clicked on "request review" button on @stuartmorgan requested changes, and accidentally removed @gmackall and @tarrinneal from review. It wasn't on purpose.

@beroso

beroso commented Mar 21, 2023 •

Copy link
Copy Markdown
Contributor Author

Have you run any benchmarks to see if/how much this is better than the old behaviour?

@tarrinneal , I just ran manual tests. I didn't do any benchmark test to compare the performance.

Before I tried to pick an 12000 x 9000 px, ~10MB image using the example app and it crashed due to an OutOfMemory error (even when the resize was not necessary).

This patch skips Bitmap decoding when resize or quality changes are not needed. Plus it can improve the resize process by subsampling image decoding depending on the maximum requested dimension.

Do you have any tips on how to benchmark it?

Comment thread packages/image_picker/image_picker_android/CHANGELOG.md Outdated

@tarrinneal tarrinneal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little nit for the changelog, tagging @stuartmorgan for second review.

@stuartmorgan-g stuartmorgan-g left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@stuartmorgan-g stuartmorgan-g added the autosubmit Merge PR when tree becomes green via auto submit App label May 10, 2023
@auto-submit
auto-submit Bot merged commit a1929a6 into flutter:main May 10, 2023
@beroso

beroso commented May 10, 2023

Copy link
Copy Markdown
Contributor Author

Thank you all for the support!

By the way, the release job is running for about 4 hours. Is it normal?

@stuartmorgan-g

Copy link
Copy Markdown
Collaborator

No, release was completely broken by the 3.10 release, it turns out. I manually released it for now.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 15, 2023
auto-submit Bot pushed a commit to flutter/flutter that referenced this pull request May 15, 2023
flutter/packages@6328cfb...6608b60

2023-05-15 [email protected] [ci] Remove unnecessary Linux libraries (flutter/packages#3975)
2023-05-15 [email protected] [ci] Enforce a minimum Kotlin version in examples (flutter/packages#3979)
2023-05-15 [email protected] [ci] Disable Windows repo tool tests (flutter/packages#3980)
2023-05-12 [email protected] [ci] Add LUCI repo tool tests (flutter/packages#3964)
2023-05-12 [email protected] [url_launcher] Set broadcast reciever visability as required by target api 34 (flutter/packages#3973)
2023-05-12 [email protected] Update build.gradle with a comment explaining version (flutter/packages#3972)
2023-05-12 [email protected] Roll Flutter from 8c5a1ea to a76dbe4 (12 revisions) (flutter/packages#3958)
2023-05-12 [email protected] [image_picker] Fix Android lints (flutter/packages#3887)
2023-05-12 [email protected] [various] Update minimum Flutter version to 3.3 (flutter/packages#3967)
2023-05-12 [email protected] [tool] Target specific Android unit tests (flutter/packages#3955)
2023-05-12 [email protected] [camerax] Add a dependency on kotlin-bom to align versions of kotlin-stdlib (flutter/packages#3960)
2023-05-12 [email protected] [local_auth] Improve iOS test DI (flutter/packages#3959)
2023-05-12 [email protected] [ci] Ensure scripts fail if a command fails (flutter/packages#3963)
2023-05-11 [email protected] [ci] Pin the Flutter version for `release` (flutter/packages#3965)
2023-05-11 [email protected] [video_player] foundation - reduce seek accuracy to fix seek to end bug (flutter/packages#3784)
2023-05-11 [email protected] [image_picker] Fix use_build_context_synchronously violations (flutter/packages#3969)
2023-05-11 [email protected] [tool] Ensure that publish credential path is available (flutter/packages#3970)
2023-05-11 [email protected] [go_router] change `fix_data.yaml` uris to be absolute to fix lints (flutter/packages#3877)
2023-05-11 [email protected] [tool] Use new pub cache location for publish (flutter/packages#3962)
2023-05-10 [email protected] Roll Flutter (stable) from f72efea to 84a1e90 (1920 revisions) (flutter/packages#3961)
2023-05-10 [email protected] [image_picker_android] Improved Bitmap resize on Android (flutter/packages#3423)
2023-05-10 [email protected] [camerax] Add Kotlin duplicate classes known error to README.md (flutter/packages#3957)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
CaseyHillers pushed a commit to CaseyHillers/flutter that referenced this pull request May 24, 2023
flutter/packages@6328cfb...6608b60

2023-05-15 [email protected] [ci] Remove unnecessary Linux libraries (flutter/packages#3975)
2023-05-15 [email protected] [ci] Enforce a minimum Kotlin version in examples (flutter/packages#3979)
2023-05-15 [email protected] [ci] Disable Windows repo tool tests (flutter/packages#3980)
2023-05-12 [email protected] [ci] Add LUCI repo tool tests (flutter/packages#3964)
2023-05-12 [email protected] [url_launcher] Set broadcast reciever visability as required by target api 34 (flutter/packages#3973)
2023-05-12 [email protected] Update build.gradle with a comment explaining version (flutter/packages#3972)
2023-05-12 [email protected] Roll Flutter from 8c5a1ea to a76dbe4 (12 revisions) (flutter/packages#3958)
2023-05-12 [email protected] [image_picker] Fix Android lints (flutter/packages#3887)
2023-05-12 [email protected] [various] Update minimum Flutter version to 3.3 (flutter/packages#3967)
2023-05-12 [email protected] [tool] Target specific Android unit tests (flutter/packages#3955)
2023-05-12 [email protected] [camerax] Add a dependency on kotlin-bom to align versions of kotlin-stdlib (flutter/packages#3960)
2023-05-12 [email protected] [local_auth] Improve iOS test DI (flutter/packages#3959)
2023-05-12 [email protected] [ci] Ensure scripts fail if a command fails (flutter/packages#3963)
2023-05-11 [email protected] [ci] Pin the Flutter version for `release` (flutter/packages#3965)
2023-05-11 [email protected] [video_player] foundation - reduce seek accuracy to fix seek to end bug (flutter/packages#3784)
2023-05-11 [email protected] [image_picker] Fix use_build_context_synchronously violations (flutter/packages#3969)
2023-05-11 [email protected] [tool] Ensure that publish credential path is available (flutter/packages#3970)
2023-05-11 [email protected] [go_router] change `fix_data.yaml` uris to be absolute to fix lints (flutter/packages#3877)
2023-05-11 [email protected] [tool] Use new pub cache location for publish (flutter/packages#3962)
2023-05-10 [email protected] Roll Flutter (stable) from f72efea to 84a1e90 (1920 revisions) (flutter/packages#3961)
2023-05-10 [email protected] [image_picker_android] Improved Bitmap resize on Android (flutter/packages#3423)
2023-05-10 [email protected] [camerax] Add Kotlin duplicate classes known error to README.md (flutter/packages#3957)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
nploi pushed a commit to nploi/packages that referenced this pull request Jul 16, 2023
Improves Bitmap load and resize on Android.

Original PR on flutter/plugins: flutter-team-archive/plugins#6947

Issue: flutter/flutter#118383
@reidbaker reidbaker mentioned this pull request Aug 4, 2023
10 of 11 tasks
creatorpiyush pushed a commit to creatorpiyush/packages that referenced this pull request Jun 10, 2026
Improves Bitmap load and resize on Android.

Original PR on flutter/plugins: flutter-team-archive/plugins#6947

Issue: flutter/flutter#118383
bisor0627 pushed a commit to bisor0627/packages that referenced this pull request Jun 19, 2026
Improves Bitmap load and resize on Android.

Original PR on flutter/plugins: flutter-team-archive/plugins#6947

Issue: flutter/flutter#118383
auto-submit Bot pushed a commit that referenced this pull request Oct 2, 2026
`ImageResizer.resizeImageIfNeeded` created a fresh `BitmapFactory.Options` and passed it to `calculateSampleSize`, which read `options.outWidth` and `options.outHeight`. That object had never been used for a decode, so both were 0 and the sample size was always 1: every resize with `maxWidth` or `maxHeight` decoded the full-size bitmap (about 48 MB for a 12 MP photo) before scaling it down. The original dimensions were already read by `readFileDimensions`, into a different `Options`.

This PR passes those original dimensions to `calculateSampleSize` instead. The sample size stays the largest power of two that still decodes at least the target size, so the output dimensions are unchanged.

The new `onResizeImageIfNeeded_whenDownscaling_shouldSubsampleFromOriginalSize` test resizes a 12x7 image to a width of 3 and checks the pixel decode uses `inSampleSize == 2` (before the fix it was 1) and that the output is still 3x2. This is not a regression: subsampling has never taken effect, since `inSampleSize` has been 1 from when it was introduced in #3423, and before that the full image was decoded too.

Fixes flutter/flutter#193036

## Pre-Review Checklist

**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot 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.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: image_picker platform-android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants