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

Commit c309275

Browse files
committed
Potential fix for image providers like OneDrive that return Uris with dots but no file extensions
1 parent d30a049 commit c309275

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

  • packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package io.flutter.plugins.imagepicker;
2525

2626
import android.annotation.SuppressLint;
27+
import android.content.ContentResolver;
2728
import android.content.ContentUris;
2829
import android.content.Context;
2930
import android.database.Cursor;
@@ -33,14 +34,21 @@
3334
import android.provider.DocumentsContract;
3435
import android.provider.MediaStore;
3536
import android.text.TextUtils;
37+
import android.webkit.MimeTypeMap;
38+
3639
import java.io.File;
3740
import java.io.FileOutputStream;
3841
import java.io.IOException;
3942
import java.io.InputStream;
4043
import java.io.OutputStream;
44+
import java.util.ArrayList;
45+
import java.util.Arrays;
46+
import java.util.List;
4147

4248
class FileUtils {
4349

50+
private static List<String> KNOWN_IMAGE_TYPES = Arrays.asList("jpg","jpeg","png","gif","svg","webp");
51+
4452
String getPathFromUri(final Context context, final Uri uri) {
4553
String path = getPathFromLocalUri(context, uri);
4654
if (path == null) {
@@ -144,7 +152,11 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
144152
OutputStream outputStream = null;
145153
boolean success = false;
146154
try {
147-
String extension = getImageExtension(uri);
155+
String extension = getImageExtensionFromUri(context, uri);
156+
157+
if (extension == null) {
158+
extension = getImageExtension(uri);
159+
}
148160
inputStream = context.getContentResolver().openInputStream(uri);
149161
file = File.createTempFile("image_picker", extension, context.getCacheDir());
150162
outputStream = new FileOutputStream(file);
@@ -170,6 +182,19 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
170182
return success ? file.getPath() : null;
171183
}
172184

185+
private static String getImageExtensionFromUri(Context context, Uri uriImage) {
186+
// Tries to somehow determine the extension from the Uri before giving up
187+
if (uriImage.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
188+
//If scheme is a content
189+
final MimeTypeMap mime = MimeTypeMap.getSingleton();
190+
final String extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uriImage));
191+
192+
return extension != null && !extension.isEmpty() ? "." + extension : null;
193+
}
194+
195+
return null;
196+
}
197+
173198
/** @return extension of image with dot, or default .jpg if it none. */
174199
private static String getImageExtension(Uri uriImage) {
175200
String extension = null;
@@ -178,6 +203,11 @@ private static String getImageExtension(Uri uriImage) {
178203
String imagePath = uriImage.getPath();
179204
if (imagePath != null && imagePath.lastIndexOf(".") != -1) {
180205
extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
206+
207+
if (extension.length() > 4 || !KNOWN_IMAGE_TYPES.contains(extension.toLowerCase())) {
208+
// Somehow the uri seems to have an extension but doesn't
209+
extension = null;
210+
}
181211
}
182212
} catch (Exception e) {
183213
extension = null;

0 commit comments

Comments
 (0)