Skip to content

Commit 2012577

Browse files
Merge pull request #1744 from miguelpruivo/feature/image-duplication-in-pictures-folder-on-android
Changed compressionQuality by default to 0
2 parents 1d93d9f + 3f0c3e4 commit 2012577

File tree

14 files changed

+54
-34
lines changed

14 files changed

+54
-34
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
## 10.0.0
2+
### General
3+
- **BREAKING CHANGE:** The `compressionQuality` property in the `pickFiles` method now defaults to `0`.
4+
- **BREAKING CHANGE:** The `allowCompression` property has been deprecated in favor of `compressionQuality`, and now defaults to `false`.
5+
6+
### Android
7+
- Fixed a permission denied exception on Android 11 or lower when `compressionQuality` is not `0`. ([#1742](https://github.com/miguelpruivo/flutter_file_picker/issues/1742)). [@vicajilau](https://github.com/vicajilau)
8+
- Fixed an issue where images were duplicated to the "Pictures" folder on Android. ([#1743](https://github.com/miguelpruivo/flutter_file_picker/issues/1743)) [@vicajilau](https://github.com/vicajilau)
9+
110
## 9.2.3
211
### Desktop (macOS)
312
- Fixed an issue when the active viewController is not a FlutterViewController.
413

514
## 9.2.2
615
### Desktop (macOS)
7-
- Updated the file picker to check for missing entitlements, instead of failing silently.
16+
- Updated the file picker to check for missing entitlements, instead of failing silently. [@vicajilau](https://github.com/vicajilau).
817

918
## 9.2.1
1019
### Desktop (macOS)

android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerDelegate.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public class FilePickerDelegate implements PluginRegistry.ActivityResultListener
4141
private boolean isMultipleSelection = false;
4242
private boolean loadDataToMemory = false;
4343
private String type;
44-
private boolean allowCompression = true;
45-
private int compressionQuality=20;
44+
private int compressionQuality = 0;
4645
private String[] allowedExtensions;
4746
private EventChannel.EventSink eventSink;
4847

@@ -122,7 +121,7 @@ public void run() {
122121
while (currentItem < count) {
123122
Uri currentUri = data.getClipData().getItemAt(currentItem).getUri();
124123

125-
if (Objects.equals(type, "image/*") && allowCompression && compressionQuality > 0) {
124+
if (Objects.equals(type, "image/*") && compressionQuality > 0) {
126125
currentUri = FileUtils.compressImage(currentUri, compressionQuality, activity.getApplicationContext());
127126
}
128127
final FileInfo file = FileUtils.openFileStream(FilePickerDelegate.this.activity, currentUri, loadDataToMemory);
@@ -137,7 +136,7 @@ public void run() {
137136
} else if (data.getData() != null) {
138137
Uri uri = data.getData();
139138

140-
if (Objects.equals(type, "image/*") && allowCompression && compressionQuality > 0) {
139+
if (Objects.equals(type, "image/*") && compressionQuality > 0) {
141140
uri = FileUtils.compressImage(uri, compressionQuality, activity.getApplicationContext());
142141
}
143142

@@ -279,7 +278,7 @@ private void startFileExplorer() {
279278
}
280279

281280
@SuppressWarnings("deprecation")
282-
public void startFileExplorer(final String type, final boolean isMultipleSelection, final boolean withData, final String[] allowedExtensions, final boolean allowCompression, final int compressionQuality, final MethodChannel.Result result) {
281+
public void startFileExplorer(final String type, final boolean isMultipleSelection, final boolean withData, final String[] allowedExtensions, final int compressionQuality, final MethodChannel.Result result) {
283282

284283
if (!this.setPendingMethodCallAndResult(result)) {
285284
finishWithAlreadyActiveError(result);
@@ -290,8 +289,7 @@ public void startFileExplorer(final String type, final boolean isMultipleSelecti
290289
this.loadDataToMemory = withData;
291290
this.allowedExtensions = allowedExtensions;
292291
this.compressionQuality = compressionQuality;
293-
this.allowCompression = allowCompression;
294-
292+
295293
this.startFileExplorer();
296294
}
297295

android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public void onActivityStopped(final Activity activity) {
114114
private static boolean isMultipleSelection = false;
115115
private static boolean withData = false;
116116
private static int compressionQuality;
117-
private static boolean allowCompression;
118117

119118
@SuppressWarnings("unchecked")
120119
@Override
@@ -151,15 +150,14 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result rawRe
151150
} else if (fileType != "dir") {
152151
isMultipleSelection = (boolean) arguments.get("allowMultipleSelection");
153152
withData = (boolean) arguments.get("withData");
154-
allowCompression = (boolean) arguments.get("allowCompression");
155153
compressionQuality=(int) arguments.get("compressionQuality");
156154
allowedExtensions = FileUtils.getMimeTypes((ArrayList<String>) arguments.get("allowedExtensions"));
157155
}
158156

159157
if (call.method != null && call.method.equals("custom") && (allowedExtensions == null || allowedExtensions.length == 0)) {
160158
result.error(TAG, "Unsupported filter. Make sure that you are only using the extension without the dot, (ie., jpg instead of .jpg). This could also have happened because you are using an unsupported file extension. If the problem persists, you may want to consider using FileType.any instead.", null);
161159
} else {
162-
this.delegate.startFileExplorer(fileType, isMultipleSelection, withData, allowedExtensions, allowCompression, compressionQuality, result);
160+
this.delegate.startFileExplorer(fileType, isMultipleSelection, withData, allowedExtensions, compressionQuality, result);
163161
}
164162

165163
}

android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.ArrayList;
3535
import java.util.Date;
3636
import java.util.HashMap;
37+
import java.util.Locale;
3738
import java.util.Random;
3839

3940
public class FileUtils {
@@ -95,7 +96,7 @@ public static String getFileName(Uri uri, final Context context) {
9596
public static Uri compressImage(Uri originalImageUri, int compressionQuality, Context context) {
9697
Uri compressedUri;
9798
try (InputStream imageStream = context.getContentResolver().openInputStream(originalImageUri)) {
98-
File compressedFile = createImageFile();
99+
File compressedFile = createImageFile(context);
99100
Bitmap originalBitmap = BitmapFactory.decodeStream(imageStream);
100101
// Compress and save the image
101102
FileOutputStream fos = new FileOutputStream(compressedFile);
@@ -113,10 +114,10 @@ public static Uri compressImage(Uri originalImageUri, int compressionQuality, Co
113114
return compressedUri;
114115
}
115116

116-
private static File createImageFile() throws IOException {
117-
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
117+
private static File createImageFile(Context context) throws IOException {
118+
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
118119
String imageFileName = "JPEG_" + timeStamp + "_";
119-
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
120+
File storageDir = context.getCacheDir();
120121
return File.createTempFile(imageFileName, ".jpg", storageDir);
121122
}
122123

example/lib/src/file_picker_demo.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class _FilePickerDemoState extends State<FilePickerDemo> {
3838
try {
3939
_directoryPath = null;
4040
_paths = (await FilePicker.platform.pickFiles(
41-
compressionQuality: 30,
4241
type: _pickingType,
4342
allowMultiple: _multiPick,
4443
onFileLoading: (FilePickerStatus status) => print(status),

ios/file_picker/Sources/file_picker/FilePickerPlugin.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ @interface FilePickerPlugin()
2222
@property (nonatomic) MPMediaPickerController *audioPickerController;
2323
@property (nonatomic) NSArray<NSString *> * allowedExtensions;
2424
@property (nonatomic) BOOL loadDataToMemory;
25+
@property (nonatomic) int compressionQuality;
2526
@property (nonatomic) BOOL allowCompression;
2627
@property (nonatomic) dispatch_group_t group;
2728
@property (nonatomic) MediaType type;
@@ -114,8 +115,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
114115

115116
NSDictionary * arguments = call.arguments;
116117
BOOL isMultiplePick = ((NSNumber*)[arguments valueForKey:@"allowMultipleSelection"]).boolValue;
117-
118-
self.allowCompression = ((NSNumber*)[arguments valueForKey:@"allowCompression"]).boolValue;
118+
119+
int compressionQuality = [[arguments valueForKey:@"compressionQuality"] intValue];
120+
self.allowCompression = self.compressionQuality > 0;
119121
self.loadDataToMemory = ((NSNumber*)[arguments valueForKey:@"withData"]).boolValue;
120122

121123
if([call.method isEqualToString:@"any"] || [call.method containsString:@"custom"]) {

ios/file_picker/Sources/file_picker/ImageUtils.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ + (BOOL)hasAlpha:(UIImage *)image {
1717
alpha == kCGImageAlphaPremultipliedFirst || alpha == kCGImageAlphaPremultipliedLast);
1818
}
1919

20-
// Save the image temporarly in the app's tmp directory
20+
// Temporarily save the image in the app's tmp directory.
2121
+ (NSURL *)saveTmpImage:(UIImage *)image {
2222
BOOL hasAlpha = [ImageUtils hasAlpha:image];
2323
NSData *data = hasAlpha ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, 1.0);

lib/_internal/file_picker_web.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ class FilePickerWeb extends FilePicker {
4343
List<String>? allowedExtensions,
4444
bool allowMultiple = false,
4545
Function(FilePickerStatus)? onFileLoading,
46-
bool allowCompression = true,
46+
@Deprecated(
47+
'allowCompression is deprecated and has no effect. Use compressionQuality instead.')
48+
bool allowCompression = false,
4749
bool withData = true,
4850
bool withReadStream = false,
4951
bool lockParentWindow = false,
5052
bool readSequential = false,
51-
int compressionQuality = 20,
53+
int compressionQuality = 0,
5254
}) async {
5355
if (type != FileType.custom && (allowedExtensions?.isNotEmpty ?? false)) {
5456
throw Exception(

lib/src/file_picker.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ abstract class FilePicker extends PlatformInterface {
6262
/// Not supported on macOS.
6363
///
6464
/// If [allowCompression] is set, it will allow media to apply the default OS compression.
65-
/// Defaults to `true`.
65+
/// Defaults to `false`.
66+
/// **Deprecated:** This option has no effect. Use [compressionQuality] instead.
6667
///
6768
/// If [lockParentWindow] is set, the child window (file picker window) will
6869
/// stay in front of the Flutter window until it is closed (like a modal
@@ -95,8 +96,10 @@ abstract class FilePicker extends PlatformInterface {
9596
FileType type = FileType.any,
9697
List<String>? allowedExtensions,
9798
Function(FilePickerStatus)? onFileLoading,
98-
bool allowCompression = true,
99-
int compressionQuality = 30,
99+
@Deprecated(
100+
'allowCompression is deprecated and has no effect. Use compressionQuality instead.')
101+
bool allowCompression = false,
102+
int compressionQuality = 0,
100103
bool allowMultiple = false,
101104
bool withData = false,
102105
bool withReadStream = false,

lib/src/file_picker_io.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ class FilePickerIO extends FilePicker {
3030
String? dialogTitle,
3131
String? initialDirectory,
3232
Function(FilePickerStatus)? onFileLoading,
33-
bool? allowCompression = true,
33+
@Deprecated(
34+
'allowCompression is deprecated and has no effect. Use compressionQuality instead.')
35+
bool? allowCompression = false,
3436
bool allowMultiple = false,
3537
bool? withData = false,
36-
int compressionQuality = 30,
38+
int compressionQuality = 0,
3739
bool? withReadStream = false,
3840
bool lockParentWindow = false,
3941
bool readSequential = false,
@@ -93,7 +95,7 @@ class FilePickerIO extends FilePicker {
9395
_eventSubscription?.cancel();
9496
if (onFileLoading != null) {
9597
_eventSubscription = _eventChannel.receiveBroadcastStream().listen(
96-
(data) => onFileLoading((data as bool)
98+
(data) => onFileLoading((data is bool)
9799
? FilePickerStatus.picking
98100
: FilePickerStatus.done),
99101
onError: (error) => throw Exception(error),

0 commit comments

Comments
 (0)