Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a1fd1e1

Browse files
authored
Avoid crashing when FlutterImageView is resized to zero dimension (#27946)
1 parent c2f2291 commit a1fd1e1

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

shell/platform/android/io/flutter/embedding/android/FlutterImageView.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import androidx.annotation.NonNull;
2121
import androidx.annotation.Nullable;
2222
import androidx.annotation.VisibleForTesting;
23+
import io.flutter.Log;
2324
import io.flutter.embedding.engine.renderer.FlutterRenderer;
2425
import io.flutter.embedding.engine.renderer.RenderSurface;
2526
import java.nio.ByteBuffer;
27+
import java.util.Locale;
2628

2729
/**
2830
* Paints a Flutter UI provided by an {@link android.media.ImageReader} onto a {@link
@@ -38,11 +40,17 @@
3840
*/
3941
@TargetApi(19)
4042
public class FlutterImageView extends View implements RenderSurface {
43+
private static final String TAG = "FlutterImageView";
44+
4145
@NonNull private ImageReader imageReader;
4246
@Nullable private Image currentImage;
4347
@Nullable private Bitmap currentBitmap;
4448
@Nullable private FlutterRenderer flutterRenderer;
4549

50+
public ImageReader getImageReader() {
51+
return imageReader;
52+
}
53+
4654
public enum SurfaceKind {
4755
/** Displays the background canvas. */
4856
background,
@@ -86,9 +94,21 @@ private void init() {
8694
setAlpha(0.0f);
8795
}
8896

97+
private static void logW(String format, Object... args) {
98+
Log.w(TAG, String.format(Locale.US, format, args));
99+
}
100+
89101
@TargetApi(19)
90102
@NonNull
91103
private static ImageReader createImageReader(int width, int height) {
104+
if (width <= 0) {
105+
logW("ImageReader width must be greater than 0, but given width=%d, set width=1", width);
106+
width = 1;
107+
}
108+
if (height <= 0) {
109+
logW("ImageReader height must be greater than 0, but given height=%d, set height=1", height);
110+
height = 1;
111+
}
92112
if (android.os.Build.VERSION.SDK_INT >= 29) {
93113
return ImageReader.newInstance(
94114
width,

shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,31 @@ public void flutterImageView_detachFromRendererClosesPreviousImage() {
734734
verify(mockImage, times(3)).close();
735735
}
736736

737+
@Test
738+
public void flutterImageView_workaroundWithOnePixelWhenResizeWithZero() {
739+
final ImageReader mockReader = mock(ImageReader.class);
740+
when(mockReader.getMaxImages()).thenReturn(2);
741+
742+
final FlutterImageView imageView =
743+
spy(
744+
new FlutterImageView(
745+
RuntimeEnvironment.application,
746+
mockReader,
747+
FlutterImageView.SurfaceKind.background));
748+
749+
final FlutterJNI jni = mock(FlutterJNI.class);
750+
imageView.attachToRenderer(new FlutterRenderer(jni));
751+
752+
final Image mockImage = mock(Image.class);
753+
when(mockReader.acquireLatestImage()).thenReturn(mockImage);
754+
755+
final int incorrectWidth = 0;
756+
final int incorrectHeight = -100;
757+
imageView.resizeIfNeeded(incorrectWidth, incorrectHeight);
758+
assertEquals(1, imageView.getImageReader().getWidth());
759+
assertEquals(1, imageView.getImageReader().getHeight());
760+
}
761+
737762
@Test
738763
public void flutterSurfaceView_GathersTransparentRegion() {
739764
final Region mockRegion = mock(Region.class);

0 commit comments

Comments
 (0)