Skip to content

Commit e84b49c

Browse files
authored
[image_picker] Fix crash due to SecurityException (#4004)
Issue flutter/flutter#100025 mentions crashing of the image picker plugin due to a `SecurityException`. As research into the issue did not yield reproduction steps, we decided to surround the breaking method call with a `try/catch` block for now (see discussion in the issue). This PR implements just that. Instead of crashing on a `SecurityException`, the plugin will now return an image path of `null`. This PR fixes flutter/flutter#100025.
1 parent 1e214d7 commit e84b49c

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

packages/image_picker/image_picker_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.6+16
2+
3+
* Fixes crashes caused by `SecurityException` when calling `getPathFromUri()`.
4+
15
## 0.8.6+15
26

37
* Bumps androidx.activity:activity from 1.6.1 to 1.7.0.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ String getPathFromUri(final Context context, final Uri uri) {
7979
// target file was written in full. Flushing the stream merely moves
8080
// the bytes into the OS, not necessarily to the file.
8181
return null;
82+
} catch (SecurityException e) {
83+
// Calling `ContentResolver#openInputStream()` has been reported to throw a
84+
// `SecurityException` on some devices in certain circumstances. Instead of crashing, we
85+
// return `null`.
86+
//
87+
// See https://github.com/flutter/flutter/issues/100025 for more details.
88+
return null;
8289
}
8390
}
8491

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
package io.flutter.plugins.imagepicker;
66

77
import static java.nio.charset.StandardCharsets.UTF_8;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNull;
810
import static org.junit.Assert.assertTrue;
11+
import static org.mockito.ArgumentMatchers.any;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.when;
914
import static org.robolectric.Shadows.shadowOf;
1015

1116
import android.content.ContentProvider;
17+
import android.content.ContentResolver;
1218
import android.content.ContentValues;
1319
import android.content.Context;
1420
import android.database.Cursor;
@@ -66,7 +72,22 @@ public void FileUtil_GetPathFromUri() throws IOException {
6672

6773
assertTrue(bytes.length > 0);
6874
String imageStream = new String(bytes, UTF_8);
69-
assertTrue(imageStream.equals("imageStream"));
75+
assertEquals("imageStream", imageStream);
76+
}
77+
78+
@Test
79+
public void FileUtil_GetPathFromUri_securityException() throws IOException {
80+
Uri uri = Uri.parse("content://dummy/dummy.png");
81+
82+
ContentResolver mockContentResolver = mock(ContentResolver.class);
83+
when(mockContentResolver.openInputStream(any(Uri.class))).thenThrow(SecurityException.class);
84+
85+
Context mockContext = mock(Context.class);
86+
when(mockContext.getContentResolver()).thenReturn(mockContentResolver);
87+
88+
String path = fileUtils.getPathFromUri(mockContext, uri);
89+
90+
assertNull(path);
7091
}
7192

7293
@Test

packages/image_picker/image_picker_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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
55

6-
version: 0.8.6+15
6+
version: 0.8.6+16
77

88
environment:
99
sdk: ">=2.18.0 <4.0.0"

0 commit comments

Comments
 (0)