Skip to content

Commit 0fdf05f

Browse files
authored
[image_picker_android] Modify FileUtils.getBaseName to return the whole filename when it contains no period (#4237)
Fixes flutter/flutter#124821
1 parent 3b2c441 commit 0fdf05f

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-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.7+2
2+
3+
* Fixes a crash case when picking an image with a display name that does not contain a period.
4+
15
## 0.8.7+1
26

37
* Bumps org.jetbrains.kotlin:kotlin-bom from 1.8.21 to 1.8.22.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ private static void copy(InputStream in, OutputStream out) throws IOException {
137137
}
138138

139139
private static String getBaseName(String fileName) {
140+
int lastDotIndex = fileName.lastIndexOf('.');
141+
if (lastDotIndex < 0) {
142+
return fileName;
143+
}
140144
// Basename is everything before the last '.'.
141-
return fileName.substring(0, fileName.lastIndexOf('.'));
145+
return fileName.substring(0, lastDotIndex);
142146
}
143147
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ public void FileUtil_getImageName() throws IOException {
109109
assertTrue(path.endsWith("a.b.png"));
110110
}
111111

112+
@Test
113+
public void FileUtil_getPathFromUri_noExtensionInBaseName() throws IOException {
114+
Uri uri = MockContentProvider.NO_EXTENSION_URI;
115+
Robolectric.buildContentProvider(MockContentProvider.class).create("dummy");
116+
shadowContentResolver.registerInputStream(
117+
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8)));
118+
String path = fileUtils.getPathFromUri(context, uri);
119+
assertTrue(path.endsWith("abc.png"));
120+
}
121+
112122
@Test
113123
public void FileUtil_getImageName_mismatchedType() throws IOException {
114124
Uri uri = MockContentProvider.WEBP_URI;
@@ -133,6 +143,7 @@ private static class MockContentProvider extends ContentProvider {
133143
public static final Uri PNG_URI = Uri.parse("content://dummy/a.b.png");
134144
public static final Uri WEBP_URI = Uri.parse("content://dummy/c.d.png");
135145
public static final Uri UNKNOWN_URI = Uri.parse("content://dummy/e.f.g");
146+
public static final Uri NO_EXTENSION_URI = Uri.parse("content://dummy/abc");
136147

137148
@Override
138149
public boolean onCreate() {
@@ -157,6 +168,7 @@ public Cursor query(
157168
public String getType(@NonNull Uri uri) {
158169
if (uri.equals(PNG_URI)) return "image/png";
159170
if (uri.equals(WEBP_URI)) return "image/webp";
171+
if (uri.equals(NO_EXTENSION_URI)) return "image/png";
160172
return null;
161173
}
162174

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.7+1
6+
version: 0.8.7+2
77

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

0 commit comments

Comments
 (0)