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

Commit 36b9fc8

Browse files
AmolGangadhareChris Yang
authored andcommitted
[image_picker] android: use getFileExtensionFromUrl to get extension instead of cursor(#1804)
1 parent 61e8449 commit 36b9fc8

3 files changed

Lines changed: 14 additions & 19 deletions

File tree

packages/image_picker/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.0+13
2+
3+
* Bugfix Android: Fix a crash occurs in some scenarios when user picks up image from gallery.
4+
15
## 0.6.0+12
26

37
* Use class instead of struct for `GIFInfo` in iOS implementation.

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

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import android.provider.DocumentsContract;
3030
import android.provider.MediaStore;
3131
import android.text.TextUtils;
32-
import android.webkit.MimeTypeMap;
3332
import java.io.File;
3433
import java.io.FileOutputStream;
3534
import java.io.IOException;
@@ -141,7 +140,7 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
141140
OutputStream outputStream = null;
142141
boolean success = false;
143142
try {
144-
String extension = getImageExtension(context, uri);
143+
String extension = getImageExtension(uri);
145144
inputStream = context.getContentResolver().openInputStream(uri);
146145
file = File.createTempFile("image_picker", extension, context.getCacheDir());
147146
outputStream = new FileOutputStream(file);
@@ -168,31 +167,23 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
168167
}
169168

170169
/** @return extension of image with dot, or default .jpg if it none. */
171-
private static String getImageExtension(Context context, Uri uriImage) {
170+
private static String getImageExtension(Uri uriImage) {
172171
String extension = null;
173-
Cursor cursor = null;
174172

175173
try {
176-
cursor =
177-
context
178-
.getContentResolver()
179-
.query(uriImage, new String[] {MediaStore.MediaColumns.MIME_TYPE}, null, null, null);
180-
181-
if (cursor != null && cursor.moveToNext()) {
182-
String mimeType = cursor.getString(0);
183-
184-
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
185-
}
186-
} finally {
187-
if (cursor != null) {
188-
cursor.close();
174+
String imagePath = uriImage.getPath();
175+
if (imagePath != null && imagePath.lastIndexOf(".") != -1) {
176+
extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
189177
}
178+
} catch (Exception e) {
179+
extension = null;
190180
}
191181

192-
if (extension == null) {
182+
if (extension == null || extension.isEmpty()) {
193183
//default extension for matches the previous behavior of the plugin
194184
extension = "jpg";
195185
}
186+
196187
return "." + extension;
197188
}
198189

packages/image_picker/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors:
55
- Flutter Team <flutter-dev@googlegroups.com>
66
- Rhodes Davis Jr. <rody.davis.jr@gmail.com>
77
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker
8-
version: 0.6.0+12
8+
version: 0.6.0+13
99

1010
flutter:
1111
plugin:

0 commit comments

Comments
 (0)