Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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
5 changes: 5 additions & 0 deletions packages/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 84 additions & 9 deletions packages/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -41,6 +43,10 @@ class _MyHomePageState extends State<MyHomePage> {
VideoPlayerController _controller;
String _retrieveDataError;

final TextEditingController maxWidthController = TextEditingController();
final TextEditingController maxHeightController = TextEditingController();
final TextEditingController qualityController = TextEditingController();

Future<void> _playVideo(File file) async {
if (file != null && mounted) {
await _disposeVideoController();
Expand All @@ -53,20 +59,27 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

void _onImageButtonPressed(ImageSource source) async {
void _onImageButtonPressed(ImageSource source, {BuildContext context}) async {
if (_controller != null) {
await _controller.setVolume(0.0);
}
if (isVideo) {
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;
}
});
}
}

Expand All @@ -82,6 +95,9 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void dispose() {
_disposeVideoController();
maxWidthController.dispose();
maxHeightController.dispose();
qualityController.dispose();
super.dispose();
}

Expand Down Expand Up @@ -192,7 +208,7 @@ class _MyHomePageState extends State<MyHomePage> {
FloatingActionButton(
onPressed: () {
isVideo = false;
_onImageButtonPressed(ImageSource.gallery);
_onImageButtonPressed(ImageSource.gallery, context: context);
},
heroTag: 'image0',
tooltip: 'Pick Image from gallery',
Expand All @@ -203,7 +219,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: FloatingActionButton(
onPressed: () {
isVideo = false;
_onImageButtonPressed(ImageSource.camera);
_onImageButtonPressed(ImageSource.camera, context: context);
},
heroTag: 'image1',
tooltip: 'Take a Photo',
Expand Down Expand Up @@ -249,8 +265,67 @@ class _MyHomePageState extends State<MyHomePage> {
}
return null;
}

Future<void> _displayPickImageDialog(
BuildContext context, OnPickImageCallback onPick) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Add optional parameters'),
content: Column(
children: <Widget>[
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: <Widget>[
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);

Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
- Flutter Team <flutter-dev@googlegroups.com>
- Rhodes Davis Jr. <rody.davis.jr@gmail.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker
version: 0.6.2+1
version: 0.6.2+2

flutter:
plugin:
Expand Down