Skip to content

Commit 72fcf09

Browse files
authored
[image_picker] Subsample large images when resizing on Android (flutter#13038)
`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 flutter#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.
1 parent 585b3d1 commit 72fcf09

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎packages/image_picker/image_picker_android/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.8.13+25
2+
3+
* Fixes resizing with `maxWidth` or `maxHeight` always decoding the full-size
4+
image; large images are now subsampled while decoding.
5+
16
## 0.8.13+24
27

38
* Updates Robolectric to 4.17 and adds the JVM arguments it requires for unit tests.

‎packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker/ImageResizer.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ String resizeImageIfNeeded(
5454
maxHeight);
5555
BitmapFactory.Options options = new BitmapFactory.Options();
5656
options.inSampleSize =
57-
calculateSampleSize(options, (int) targetSize.getWidth(), (int) targetSize.getHeight());
57+
calculateSampleSize(
58+
(int) originalSize.getWidth(),
59+
(int) originalSize.getHeight(),
60+
(int) targetSize.getWidth(),
61+
(int) targetSize.getHeight());
5862
Bitmap bmp = decodeFile(imagePath, options);
5963
if (bmp == null) {
6064
return imagePath;
@@ -160,10 +164,7 @@ private Bitmap createScaledBitmap(Bitmap bmp, int width, int height, boolean fil
160164
* href="https://developer.android.com/topic/performance/graphics/load-bitmap#load-bitmap">
161165
* Loading Large Bitmaps Efficiently</a>
162166
*/
163-
private int calculateSampleSize(
164-
BitmapFactory.Options options, int targetWidth, int targetHeight) {
165-
final int height = options.outHeight;
166-
final int width = options.outWidth;
167+
private int calculateSampleSize(int width, int height, int targetWidth, int targetHeight) {
167168
int sampleSize = 1;
168169
if (height > targetHeight || width > targetWidth) {
169170
final int halfHeight = height / 2;

‎packages/image_picker/image_picker_android/android/src/test/java/io/flutter/plugins/imagepicker/ImageResizerTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ public void onResizeImageIfNeeded_whenResizeIsNecessary_shouldDecodeBitmapPixels
149149
}
150150
}
151151

152+
@Test
153+
public void onResizeImageIfNeeded_whenDownscaling_shouldSubsampleFromOriginalSize() {
154+
try (MockedStatic<BitmapFactory> mockBitmapFactory =
155+
mockStatic(BitmapFactory.class, Mockito.CALLS_REAL_METHODS)) {
156+
// The 12x7 image targets 3x2, so a sample size of 2 still decodes at least the target size.
157+
resizer.resizeImageIfNeeded(wideJPG.getPath(), 3.0, null, 100);
158+
ArgumentCaptor<BitmapFactory.Options> argument =
159+
ArgumentCaptor.forClass(BitmapFactory.Options.class);
160+
mockBitmapFactory.verify(
161+
() -> BitmapFactory.decodeFile(anyString(), argument.capture()), times(2));
162+
assertThat(argument.getAllValues().get(1).inSampleSize, equalTo(2));
163+
}
164+
SizeFCompat scaledSize =
165+
resizer.readFileDimensions(externalDirectory.getPath() + "/scaled_jpgImageWide.jpg");
166+
assertThat(scaledSize.getWidth(), equalTo(3.0F));
167+
assertThat(scaledSize.getHeight(), equalTo(2.0F));
168+
}
169+
152170
@Test
153171
public void
154172
onResizeImageIfNeeded_whenImageIsVertical_WidthIsGreaterThanOriginal_shouldResizeCorrectly() {

‎packages/image_picker/image_picker_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: image_picker_android
22
description: Android implementation of the image_picker plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
5-
version: 0.8.13+24
5+
version: 0.8.13+25
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)