Skip to content

[image_picker_android] Modify FileUtils.getBaseName to return the whole filename when it contains no period #4237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2023
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
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.7+2

* Fixes a crash case when picking an image with a display name that does not contain a period.

## 0.8.7+1

* Bumps org.jetbrains.kotlin:kotlin-bom from 1.8.21 to 1.8.22.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ private static void copy(InputStream in, OutputStream out) throws IOException {
}

private static String getBaseName(String fileName) {
int lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex < 0) {
return fileName;
}
// Basename is everything before the last '.'.
return fileName.substring(0, fileName.lastIndexOf('.'));
return fileName.substring(0, lastDotIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ public void FileUtil_getImageName() throws IOException {
assertTrue(path.endsWith("a.b.png"));
}

@Test
public void FileUtil_getPathFromUri_noExtensionInBaseName() throws IOException {
Uri uri = MockContentProvider.NO_EXTENSION_URI;
Robolectric.buildContentProvider(MockContentProvider.class).create("dummy");
shadowContentResolver.registerInputStream(
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8)));
String path = fileUtils.getPathFromUri(context, uri);
assertTrue(path.endsWith("abc.png"));
}

@Test
public void FileUtil_getImageName_mismatchedType() throws IOException {
Uri uri = MockContentProvider.WEBP_URI;
Expand All @@ -133,6 +143,7 @@ private static class MockContentProvider extends ContentProvider {
public static final Uri PNG_URI = Uri.parse("content://dummy/a.b.png");
public static final Uri WEBP_URI = Uri.parse("content://dummy/c.d.png");
public static final Uri UNKNOWN_URI = Uri.parse("content://dummy/e.f.g");
public static final Uri NO_EXTENSION_URI = Uri.parse("content://dummy/abc");

@Override
public boolean onCreate() {
Expand All @@ -157,6 +168,7 @@ public Cursor query(
public String getType(@NonNull Uri uri) {
if (uri.equals(PNG_URI)) return "image/png";
if (uri.equals(WEBP_URI)) return "image/webp";
if (uri.equals(NO_EXTENSION_URI)) return "image/png";
return null;
}

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 @@ -3,7 +3,7 @@ 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.7+1
version: 0.8.7+2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down