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

[image_picker] Implemented 2860 and added Unit Test to test functionality #3685

Merged
merged 10 commits into from
Mar 10, 2021
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/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.7.2+1

* Android: fixes an issue where videos could be wrongly picked with `.jpg` extension.

## 0.7.2

* Run CocoaPods iOS tests in RunnerUITests target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

package io.flutter.plugins.imagepicker;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.webkit.MimeTypeMap;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -39,7 +41,7 @@ String getPathFromUri(final Context context, final Uri uri) {
OutputStream outputStream = null;
boolean success = false;
try {
String extension = getImageExtension(uri);
String extension = getImageExtension(context, uri);
inputStream = context.getContentResolver().openInputStream(uri);
file = File.createTempFile("image_picker", extension, context.getCacheDir());
file.deleteOnExit();
Expand Down Expand Up @@ -67,13 +69,18 @@ String getPathFromUri(final Context context, final Uri uri) {
}

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

try {
String imagePath = uriImage.getPath();
if (imagePath != null && imagePath.lastIndexOf(".") != -1) {
extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
if (uriImage.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uriImage));
} else {
extension =
MimeTypeMap.getFileExtensionFromUrl(
Uri.fromFile(new File(uriImage.getPath())).toString());
}
} catch (Exception e) {
extension = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ public void FileUtil_GetPathFromUri() throws IOException {
String imageStream = new String(bytes, UTF_8);
assertTrue(imageStream.equals("imageStream"));
}

@Test
public void FileUtil_getImageExtension() throws IOException {
Uri uri = Uri.parse("content://dummy/dummy.png");
shadowContentResolver.registerInputStream(
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8)));
String path = fileUtils.getPathFromUri(context, uri);
assertTrue(path.endsWith(".jpg"));
}
}
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: image_picker
description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
version: 0.7.2
version: 0.7.2+1

flutter:
plugin:
Expand Down