Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.8.13+25

* Fixes resizing with `maxWidth` or `maxHeight` always decoding the full-size
image; large images are now subsampled while decoding.

## 0.8.13+24

* Updates Robolectric to 4.17 and adds the JVM arguments it requires for unit tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ String resizeImageIfNeeded(
maxHeight);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize =
calculateSampleSize(options, (int) targetSize.getWidth(), (int) targetSize.getHeight());
calculateSampleSize(
(int) originalSize.getWidth(),
(int) originalSize.getHeight(),
(int) targetSize.getWidth(),
(int) targetSize.getHeight());
Bitmap bmp = decodeFile(imagePath, options);
if (bmp == null) {
return imagePath;
Expand Down Expand Up @@ -160,10 +164,7 @@ private Bitmap createScaledBitmap(Bitmap bmp, int width, int height, boolean fil
* href="https://developer.android.com/topic/performance/graphics/load-bitmap#load-bitmap">
* Loading Large Bitmaps Efficiently</a>
*/
private int calculateSampleSize(
BitmapFactory.Options options, int targetWidth, int targetHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
private int calculateSampleSize(int width, int height, int targetWidth, int targetHeight) {
int sampleSize = 1;
if (height > targetHeight || width > targetWidth) {
Comment thread
anilcancakir marked this conversation as resolved.
final int halfHeight = height / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ public void onResizeImageIfNeeded_whenResizeIsNecessary_shouldDecodeBitmapPixels
}
}

@Test
public void onResizeImageIfNeeded_whenDownscaling_shouldSubsampleFromOriginalSize() {
try (MockedStatic<BitmapFactory> mockBitmapFactory =
mockStatic(BitmapFactory.class, Mockito.CALLS_REAL_METHODS)) {
// The 12x7 image targets 3x2, so a sample size of 2 still decodes at least the target size.
resizer.resizeImageIfNeeded(wideJPG.getPath(), 3.0, null, 100);
ArgumentCaptor<BitmapFactory.Options> argument =
ArgumentCaptor.forClass(BitmapFactory.Options.class);
mockBitmapFactory.verify(
() -> BitmapFactory.decodeFile(anyString(), argument.capture()), times(2));
assertThat(argument.getAllValues().get(1).inSampleSize, equalTo(2));
}
SizeFCompat scaledSize =
resizer.readFileDimensions(externalDirectory.getPath() + "/scaled_jpgImageWide.jpg");
assertThat(scaledSize.getWidth(), equalTo(3.0F));
assertThat(scaledSize.getHeight(), equalTo(2.0F));
}

@Test
public void
onResizeImageIfNeeded_whenImageIsVertical_WidthIsGreaterThanOriginal_shouldResizeCorrectly() {
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: image_picker_android
description: Android implementation of the image_picker plugin.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.13+24
version: 0.8.13+25

environment:
sdk: ^3.12.0
Expand Down
Loading