Skip to content

Commit 4ffbf7d

Browse files
committed
feat-add-mime-type
1 parent 36425c7 commit 4ffbf7d

File tree

7 files changed

+26
-12
lines changed

7 files changed

+26
-12
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public void startFileExplorer(final String type, final boolean isMultipleSelecti
290290
}
291291

292292
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
293-
public void saveFile(String fileName, String type, String initialDirectory, String[] allowedExtensions, byte[] bytes, MethodChannel.Result result) {
293+
public void saveFile(String fileName, String type, String mimeType, String initialDirectory, String[] allowedExtensions, byte[] bytes, MethodChannel.Result result) {
294294
if (!this.setPendingMethodCallAndResult(result)) {
295295
finishWithAlreadyActiveError(result);
296296
return;
@@ -301,7 +301,9 @@ public void saveFile(String fileName, String type, String initialDirectory, Stri
301301
intent.putExtra(Intent.EXTRA_TITLE, fileName);
302302
}
303303
this.bytes = bytes;
304-
if (type != null && !"dir".equals(type) && type.split(",").length == 1) {
304+
if (mimeType != null) {
305+
intent.setType(mimeType);
306+
} else if (type != null && !"dir".equals(type) && type.split(",").length == 1) {
305307
intent.setType(type);
306308
} else {
307309
intent.setType("*/*");

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result rawRe
134134

135135
if (call.method != null && call.method.equals("save")) {
136136
String fileName = (String) arguments.get("fileName");
137-
String type = resolveType((String) arguments.get("fileType"));
137+
String type = (String) arguments.get("fileType");
138+
String mimeType = (String) arguments.get("mimeType");
138139
String initialDirectory = (String) arguments.get("initialDirectory");
139140
String[] allowedExtensions = FileUtils.getMimeTypes((ArrayList<String>) arguments.get("allowedExtensions"));
140141
byte[] bytes = (byte[]) arguments.get("bytes");
141-
this.delegate.saveFile(fileName, type, initialDirectory, allowedExtensions, bytes,result);
142+
this.delegate.saveFile(fileName, type, mimeType, initialDirectory, allowedExtensions, bytes,result);
142143
return;
143144
}
144145

lib/src/file_picker.dart

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ abstract class FilePicker extends PlatformInterface {
162162
/// stay in front of the Flutter window until it is closed (like a modal
163163
/// window). This parameter works only on Windows desktop.
164164
///
165+
/// [mimeType] can be optionally set to provide a mime type for the file.
166+
/// It only work for iOS and Android.
167+
///
165168
/// Returns `null` if aborted. Returns a [Future<String?>] which resolves to
166169
/// the absolute path of the selected file, if the user selected a file.
167170
Future<String?> saveFile({
@@ -172,6 +175,7 @@ abstract class FilePicker extends PlatformInterface {
172175
List<String>? allowedExtensions,
173176
Uint8List? bytes,
174177
bool lockParentWindow = false,
178+
String? mimeType,
175179
}) async =>
176180
throw UnimplementedError('saveFile() has not been implemented.');
177181
}

lib/src/file_picker_io.dart

+12-8
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,20 @@ class FilePickerIO extends FilePicker {
137137
}
138138

139139
@override
140-
Future<String?> saveFile(
141-
{String? dialogTitle,
142-
String? fileName,
143-
String? initialDirectory,
144-
FileType type = FileType.any,
145-
List<String>? allowedExtensions,
146-
Uint8List? bytes,
147-
bool lockParentWindow = false}) {
140+
Future<String?> saveFile({
141+
String? dialogTitle,
142+
String? fileName,
143+
String? initialDirectory,
144+
FileType type = FileType.any,
145+
List<String>? allowedExtensions,
146+
Uint8List? bytes,
147+
bool lockParentWindow = false,
148+
String? mimeType,
149+
}) {
148150
if (Platform.isIOS || Platform.isAndroid) {
149151
return _channel.invokeMethod("save", {
150152
"fileName": fileName,
153+
"mimeType": mimeType,
151154
"fileType": type.name,
152155
"initialDirectory": initialDirectory,
153156
"allowedExtensions": allowedExtensions,
@@ -162,6 +165,7 @@ class FilePickerIO extends FilePicker {
162165
allowedExtensions: allowedExtensions,
163166
bytes: bytes,
164167
lockParentWindow: lockParentWindow,
168+
mimeType: mimeType,
165169
);
166170
}
167171
}

lib/src/file_picker_macos.dart

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class FilePickerMacOS extends FilePicker {
8989
List<String>? allowedExtensions,
9090
Uint8List? bytes,
9191
bool lockParentWindow = false,
92+
String? mimeType,
9293
}) async {
9394
final String executable = await isExecutableOnPath('osascript');
9495
final String fileFilter = fileTypeToFileFilter(

lib/src/linux/file_picker_linux.dart

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class FilePickerLinux extends FilePicker {
8787
List<String>? allowedExtensions,
8888
Uint8List? bytes,
8989
bool lockParentWindow = false,
90+
String? mimeType,
9091
}) async {
9192
final executable = await _getPathToExecutable();
9293
final dialogHandler = DialogHandler(executable);

lib/src/windows/file_picker_windows.dart

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class FilePickerWindows extends FilePicker {
172172
List<String>? allowedExtensions,
173173
Uint8List? bytes,
174174
bool lockParentWindow = false,
175+
String? mimeType,
175176
}) async {
176177
final port = ReceivePort();
177178
await Isolate.spawn(

0 commit comments

Comments
 (0)