[image_picker] Fix file not found exception (#2341)
This PR adds a check to ensure the directory where scaled images are saved to exist. This issue caused the app to crash on a Xiaomi Mi 9T (FileNotFoundException).
diff --git a/AUTHORS b/AUTHORS
index ffc4e5a..47038c2 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -44,9 +44,10 @@
SoundReply Solutions GmbH <ch@soundreply.com>
Rafal Wachol <rwachol@gmail.com>
Pau Picas <pau.picas@gmail.com>
+Christian Weder <chrstian.weder@yapeal.ch>
Rhodes Davis Jr. <rody.davis.jr@gmail.com>
Luigi Agosti <luigi@tengio.com>
Quentin Le Guennec <quentin@tengio.com>
Koushik Ravikumar <koushik@tengio.com>
Nissim Dsilva <nissim@tengio.com>
-Giancarlo Rocha <giancarloiff@gmail.com>
+Giancarlo Rocha <giancarloiff@gmail.com>
\ No newline at end of file
diff --git a/packages/image_picker/CHANGELOG.md b/packages/image_picker/CHANGELOG.md
index 0cd83a0..42c8a74 100644
--- a/packages/image_picker/CHANGELOG.md
+++ b/packages/image_picker/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.6.3+3
+
+* Android: Fix a crash when `externalFilesDirectory` does not exist.
+
## 0.6.3+2
* Bump RoboElectric dependency to 4.3.1 and update resource usage.
@@ -12,11 +16,13 @@
* Migrate to using the new e2e test binding.
## 0.6.2+3
+
* Remove the deprecated `author:` field from pubspec.yaml
* Migrate the plugin to the pubspec platforms manifest.
* Require Flutter SDK 1.10.0 or greater.
## 0.6.2+2
+
* Android: Revert the image file return logic when the image doesn't have to be scaled. Fix a rotation regression caused by 0.6.2+1
* Example App: Add a dialog to enter `maxWidth`, `maxHeight` or `quality` when picking image.
diff --git a/packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImageResizer.java b/packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImageResizer.java
index cf46b75..27a1455 100644
--- a/packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImageResizer.java
+++ b/packages/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImageResizer.java
@@ -105,7 +105,11 @@
}
private File createFile(File externalFilesDirectory, String child) {
- return new File(externalFilesDirectory, child);
+ File image = new File(externalFilesDirectory, child);
+ if (!image.getParentFile().exists()) {
+ image.getParentFile().mkdirs();
+ }
+ return image;
}
private FileOutputStream createOutputStream(File imageFile) throws IOException {
diff --git a/packages/image_picker/example/android/app/src/test/java/io/flutter/plugins/imagepicker/ImageResizerTest.java b/packages/image_picker/example/android/app/src/test/java/io/flutter/plugins/imagepicker/ImageResizerTest.java
index 60bcc56..4968d84 100644
--- a/packages/image_picker/example/android/app/src/test/java/io/flutter/plugins/imagepicker/ImageResizerTest.java
+++ b/packages/image_picker/example/android/app/src/test/java/io/flutter/plugins/imagepicker/ImageResizerTest.java
@@ -62,4 +62,12 @@
String outoutFile = resizer.resizeImageIfNeeded(imageFile.getPath(), null, 50.0, null);
assertThat(outoutFile, equalTo(externalDirectory.getPath() + "/scaled_pngImage.png"));
}
+
+ @Test
+ public void onResizeImageIfNeeded_WhenParentDirectoryDoesNotExists_ShouldNotCrash() {
+ File nonExistentDirectory = new File(externalDirectory, "/nonExistent");
+ ImageResizer invalidResizer = new ImageResizer(nonExistentDirectory, new ExifDataCopier());
+ String outoutFile = invalidResizer.resizeImageIfNeeded(imageFile.getPath(), null, 50.0, null);
+ assertThat(outoutFile, equalTo(nonExistentDirectory.getPath() + "/scaled_pngImage.png"));
+ }
}
diff --git a/packages/image_picker/pubspec.yaml b/packages/image_picker/pubspec.yaml
index a9a3000..02b3de1 100755
--- a/packages/image_picker/pubspec.yaml
+++ b/packages/image_picker/pubspec.yaml
@@ -2,7 +2,7 @@
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
-version: 0.6.3+2
+version: 0.6.3+3
flutter:
plugin: