Skip to content

Commit c177dfc

Browse files
authored
[image_picker] Handle a null stream from openInputStream (flutter#12718)
Null safety when ContentResolver cannot open a stream. Copied from closed: flutter#12682 Fixes: flutter/flutter#191988 ## Pre-Review Checklist
1 parent 3f30a3e commit c177dfc

4 files changed

Lines changed: 30 additions & 1 deletion

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+23
2+
3+
* Fixes a crash when the content provider returns no stream for a picked
4+
image; the pick now fails with `missing_valid_image_uri` instead.
5+
16
## 0.8.13+22
27

38
* Fixes gallery image/video selection on Android API 36+ returning no paths when

‎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
@@ -61,6 +61,13 @@ class FileUtils {
6161
*/
6262
String getPathFromUri(final Context context, final Uri uri) {
6363
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
64+
if (inputStream == null) {
65+
// `ContentResolver#openInputStream()` returns null when the provider cannot serve the
66+
// item (for example after the provider crashed, or for a cloud-only photo it could not
67+
// fetch). Treat it like any other unreadable item: no path, so the caller reports
68+
// `missing_valid_image_uri` instead of crashing on the null stream.
69+
return null;
70+
}
6471
String uuid = UUID.randomUUID().toString();
6572
File targetDirectory = new File(context.getCacheDir(), uuid);
6673
targetDirectory.mkdir();

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ public void FileUtil_GetPathFromUri() throws IOException {
8484
assertEquals("imageStream", imageStream);
8585
}
8686

87+
@Test
88+
public void FileUtil_GetPathFromUri_nullStream() throws IOException {
89+
Uri uri = Uri.parse("content://dummy/dummy.png");
90+
91+
// `openInputStream` returns null when the provider cannot serve the item; this used to
92+
// reach `copy()` and crash the plugin's executor thread with a NullPointerException.
93+
ContentResolver mockContentResolver = mock(ContentResolver.class);
94+
when(mockContentResolver.openInputStream(any(Uri.class))).thenReturn(null);
95+
96+
Context mockContext = mock(Context.class);
97+
when(mockContext.getContentResolver()).thenReturn(mockContentResolver);
98+
99+
String path = fileUtils.getPathFromUri(mockContext, uri);
100+
101+
assertNull(path);
102+
}
103+
87104
@Test
88105
public void FileUtil_GetPathFromUri_securityException() throws IOException {
89106
Uri uri = Uri.parse("content://dummy/dummy.png");

‎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+22
5+
version: 0.8.13+23
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)