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

Commit 735e1a5

Browse files
authored
[image_picker] Implemented 2860 and added Unit Test to test functionality (#3685)
1 parent 4dea720 commit 735e1a5

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

packages/image_picker/image_picker/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.2+1
2+
3+
* Android: fixes an issue where videos could be wrongly picked with `.jpg` extension.
4+
15
## 0.7.2
26

37
* Run CocoaPods iOS tests in RunnerUITests target

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
package io.flutter.plugins.imagepicker;
2525

26+
import android.content.ContentResolver;
2627
import android.content.Context;
2728
import android.net.Uri;
29+
import android.webkit.MimeTypeMap;
2830
import java.io.File;
2931
import java.io.FileOutputStream;
3032
import java.io.IOException;
@@ -39,7 +41,7 @@ String getPathFromUri(final Context context, final Uri uri) {
3941
OutputStream outputStream = null;
4042
boolean success = false;
4143
try {
42-
String extension = getImageExtension(uri);
44+
String extension = getImageExtension(context, uri);
4345
inputStream = context.getContentResolver().openInputStream(uri);
4446
file = File.createTempFile("image_picker", extension, context.getCacheDir());
4547
file.deleteOnExit();
@@ -67,13 +69,18 @@ String getPathFromUri(final Context context, final Uri uri) {
6769
}
6870

6971
/** @return extension of image with dot, or default .jpg if it none. */
70-
private static String getImageExtension(Uri uriImage) {
72+
private static String getImageExtension(Context context, Uri uriImage) {
7173
String extension = null;
7274

7375
try {
7476
String imagePath = uriImage.getPath();
75-
if (imagePath != null && imagePath.lastIndexOf(".") != -1) {
76-
extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
77+
if (uriImage.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
78+
final MimeTypeMap mime = MimeTypeMap.getSingleton();
79+
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uriImage));
80+
} else {
81+
extension =
82+
MimeTypeMap.getFileExtensionFromUrl(
83+
Uri.fromFile(new File(uriImage.getPath())).toString());
7784
}
7885
} catch (Exception e) {
7986
extension = null;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,13 @@ public void FileUtil_GetPathFromUri() throws IOException {
5454
String imageStream = new String(bytes, UTF_8);
5555
assertTrue(imageStream.equals("imageStream"));
5656
}
57+
58+
@Test
59+
public void FileUtil_getImageExtension() throws IOException {
60+
Uri uri = Uri.parse("content://dummy/dummy.png");
61+
shadowContentResolver.registerInputStream(
62+
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8)));
63+
String path = fileUtils.getPathFromUri(context, uri);
64+
assertTrue(path.endsWith(".jpg"));
65+
}
5766
}

packages/image_picker/image_picker/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: image_picker
22
description: Flutter plugin for selecting images from the Android and iOS image
33
library, and taking new pictures with the camera.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
5-
version: 0.7.2
5+
version: 0.7.2+1
66

77
flutter:
88
plugin:

0 commit comments

Comments
 (0)