diff --git a/packages/image_picker/CHANGELOG.md b/packages/image_picker/CHANGELOG.md index 96bb4fcad8c3..536bde54e84b 100644 --- a/packages/image_picker/CHANGELOG.md +++ b/packages/image_picker/CHANGELOG.md @@ -1,3 +1,8 @@ +## 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. + ## 0.6.2+1 * Android: Fix a crash when a non-image file is picked. 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 ca0498bfa55b..cf46b7585359 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 @@ -33,21 +33,19 @@ String resizeImageIfNeeded( @Nullable Double maxWidth, @Nullable Double maxHeight, @Nullable Integer imageQuality) { - boolean shouldScale = - maxWidth != null || maxHeight != null || isImageQualityValid(imageQuality); - String[] pathParts = imagePath.split("/"); - String imageName = pathParts[pathParts.length - 1]; - File file; Bitmap bmp = decodeFile(imagePath); if (bmp == null) { return null; } + boolean shouldScale = + maxWidth != null || maxHeight != null || isImageQualityValid(imageQuality); + if (!shouldScale) { + return imagePath; + } try { - if (!shouldScale) { - file = createImageOnExternalDirectory(imageName, bmp, 100); - } else { - file = resizedImage(bmp, maxWidth, maxHeight, imageQuality, imageName); - } + String[] pathParts = imagePath.split("/"); + String imageName = pathParts[pathParts.length - 1]; + File file = resizedImage(bmp, maxWidth, maxHeight, imageQuality, imageName); copyExif(imagePath, file.getPath()); return file.getPath(); } catch (IOException e) { 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 aac7472eaac3..60bcc569211b 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 @@ -42,7 +42,7 @@ public void setUp() throws IOException { @Test public void onResizeImageIfNeeded_WhenQualityIsNull_ShoultNotResize_ReturnTheUnscaledFile() { String outoutFile = resizer.resizeImageIfNeeded(imageFile.getPath(), null, null, null); - assertThat(outoutFile, equalTo(externalDirectory.getPath() + "/pngImage.png")); + assertThat(outoutFile, equalTo(imageFile.getPath())); } @Test diff --git a/packages/image_picker/example/lib/main.dart b/packages/image_picker/example/lib/main.dart index b728788f5ba7..3ece64b13d65 100755 --- a/packages/image_picker/example/lib/main.dart +++ b/packages/image_picker/example/lib/main.dart @@ -8,6 +8,8 @@ import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter/src/widgets/basic.dart'; +import 'package:flutter/src/widgets/container.dart'; import 'package:image_picker/image_picker.dart'; import 'package:video_player/video_player.dart'; @@ -41,6 +43,10 @@ class _MyHomePageState extends State { VideoPlayerController _controller; String _retrieveDataError; + final TextEditingController maxWidthController = TextEditingController(); + final TextEditingController maxHeightController = TextEditingController(); + final TextEditingController qualityController = TextEditingController(); + Future _playVideo(File file) async { if (file != null && mounted) { await _disposeVideoController(); @@ -53,7 +59,7 @@ class _MyHomePageState extends State { } } - void _onImageButtonPressed(ImageSource source) async { + void _onImageButtonPressed(ImageSource source, {BuildContext context}) async { if (_controller != null) { await _controller.setVolume(0.0); } @@ -61,12 +67,19 @@ class _MyHomePageState extends State { final File file = await ImagePicker.pickVideo(source: source); await _playVideo(file); } else { - try { - _imageFile = await ImagePicker.pickImage(source: source); - setState(() {}); - } catch (e) { - _pickImageError = e; - } + await _displayPickImageDialog(context, + (double maxWidth, double maxHeight, int quality) async { + try { + _imageFile = await ImagePicker.pickImage( + source: source, + maxWidth: maxWidth, + maxHeight: maxHeight, + imageQuality: quality); + setState(() {}); + } catch (e) { + _pickImageError = e; + } + }); } } @@ -82,6 +95,9 @@ class _MyHomePageState extends State { @override void dispose() { _disposeVideoController(); + maxWidthController.dispose(); + maxHeightController.dispose(); + qualityController.dispose(); super.dispose(); } @@ -192,7 +208,7 @@ class _MyHomePageState extends State { FloatingActionButton( onPressed: () { isVideo = false; - _onImageButtonPressed(ImageSource.gallery); + _onImageButtonPressed(ImageSource.gallery, context: context); }, heroTag: 'image0', tooltip: 'Pick Image from gallery', @@ -203,7 +219,7 @@ class _MyHomePageState extends State { child: FloatingActionButton( onPressed: () { isVideo = false; - _onImageButtonPressed(ImageSource.camera); + _onImageButtonPressed(ImageSource.camera, context: context); }, heroTag: 'image1', tooltip: 'Take a Photo', @@ -249,8 +265,67 @@ class _MyHomePageState extends State { } return null; } + + Future _displayPickImageDialog( + BuildContext context, OnPickImageCallback onPick) async { + return showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: Text('Add optional parameters'), + content: Column( + children: [ + TextField( + controller: maxWidthController, + keyboardType: TextInputType.numberWithOptions(decimal: true), + decoration: + InputDecoration(hintText: "Enter maxWidth if desired"), + ), + TextField( + controller: maxHeightController, + keyboardType: TextInputType.numberWithOptions(decimal: true), + decoration: + InputDecoration(hintText: "Enter maxHeight if desired"), + ), + TextField( + controller: qualityController, + keyboardType: TextInputType.number, + decoration: + InputDecoration(hintText: "Enter quality if desired"), + ), + ], + ), + actions: [ + FlatButton( + child: const Text('CANCEL'), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + FlatButton( + child: const Text('PICK'), + onPressed: () { + double width = maxWidthController.text.length > 0 + ? double.parse(maxWidthController.text) + : null; + double height = maxHeightController.text.length > 0 + ? double.parse(maxHeightController.text) + : null; + int quality = qualityController.text.length > 0 + ? int.parse(qualityController.text) + : null; + onPick(width, height, quality); + Navigator.of(context).pop(); + }), + ], + ); + }); + } } +typedef void OnPickImageCallback( + double maxWidth, double maxHeight, int quality); + class AspectRatioVideo extends StatefulWidget { AspectRatioVideo(this.controller); diff --git a/packages/image_picker/pubspec.yaml b/packages/image_picker/pubspec.yaml index 0e47c53e6911..833cb130c4c4 100755 --- a/packages/image_picker/pubspec.yaml +++ b/packages/image_picker/pubspec.yaml @@ -5,7 +5,7 @@ authors: - Flutter Team - Rhodes Davis Jr. homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker -version: 0.6.2+1 +version: 0.6.2+2 flutter: plugin: