diff --git a/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java b/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java index f1e2c28..c80af08 100644 --- a/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java +++ b/android/src/main/java/com/mr/flutter/plugin/filepicker/FilePickerPlugin.java @@ -136,6 +136,8 @@ public class FilePickerPlugin implements MethodCallHandler { switch (type){ case "PDF": return "application/pdf"; + case "VIDEO": + return "video/*"; case "ANY": return "*/*"; default: diff --git a/example/lib/main.dart b/example/lib/main.dart index 44e5cd2..6f3ac75 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,12 +29,6 @@ class _MyAppState extends State { }); } - // Platform messages are asynchronous, so we initialize in an async method. - - // If the widget was removed from the tree while the asynchronous platform - // message was in flight, we want to discard the reply rather than calling - // setState to update our non-existent appearance. - @override Widget build(BuildContext context) { return new MaterialApp( @@ -64,15 +58,21 @@ class _MyAppState extends State { child: new Text('FROM PDF'), value: FileType.PDF, ), + new DropdownMenuItem( + child: new Text('VIDEO'), + value: FileType.VIDEO, + ), new DropdownMenuItem( child: new Text('ANY'), value: FileType.ANY, ) ], onChanged: (value) { - setState(() { - _pickingType = value; - }); + setState( + () { + _pickingType = value; + }, + ); }, ), ), diff --git a/ios/Classes/FilePickerPlugin.h b/ios/Classes/FilePickerPlugin.h index dd57275..d7d2a7d 100644 --- a/ios/Classes/FilePickerPlugin.h +++ b/ios/Classes/FilePickerPlugin.h @@ -1,4 +1,6 @@ #import +#import +#import -@interface FilePickerPlugin : NSObject +@interface FilePickerPlugin : NSObject @end diff --git a/ios/Classes/FilePickerPlugin.m b/ios/Classes/FilePickerPlugin.m index baa99c4..4823c4c 100644 --- a/ios/Classes/FilePickerPlugin.m +++ b/ios/Classes/FilePickerPlugin.m @@ -1,4 +1,5 @@ #import "FilePickerPlugin.h" +#import "FileUtils.h" @interface FilePickerPlugin() @property (nonatomic) FlutterResult result; @@ -24,30 +25,18 @@ - (instancetype)initWithViewController:(UIViewController *)viewController { self = [super init]; - if(self){ + if(self) { self.viewController = viewController; - } + return self; } -- (NSString*) resolveType:(NSString*)type { - - if ([type isEqualToString:@"PDF"]) { - return @"com.adobe.pdf"; - } - else if ([type isEqualToString:@"ANY"]) { - return @"public.item"; - } else { - return nil; - } - -} - - (void)initPicker { + self.pickerController = [[UIDocumentPickerViewController alloc] - initWithDocumentTypes:@[self.fileType] - inMode:UIDocumentPickerModeImport]; + initWithDocumentTypes:@[self.fileType] + inMode:UIDocumentPickerModeImport]; self.pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; self.pickerController.delegate = self; @@ -61,20 +50,26 @@ _result = nil; } - self.fileType = [self resolveType:call.method]; + _result = result; - if(self.fileType == nil){ - result(FlutterMethodNotImplemented); - } else { - - [self initPicker]; - _result = result; - [_viewController presentViewController:self.pickerController animated:YES completion:^{ - if (@available(iOS 11.0, *)) { - self.pickerController.allowsMultipleSelection = NO; - } - }]; + + if([call.method isEqualToString:@"VIDEO"]) { + [self resolvePickVideo]; + } + else { + self.fileType = [FileUtils resolveType:call.method]; + if(self.fileType == nil){ + result(FlutterMethodNotImplemented); + } else { + [self initPicker]; + [_viewController presentViewController:self.pickerController animated:YES completion:^{ + if (@available(iOS 11.0, *)) { + self.pickerController.allowsMultipleSelection = NO; + } + }]; + + } } } @@ -83,35 +78,32 @@ didPickDocumentsAtURLs:(NSArray *)urls{ [self.pickerController dismissViewControllerAnimated:YES completion:nil]; - - NSString * uri; - - for (NSURL *url in urls) { - uri = (NSString *)[url path]; - } - - _result(uri); + _result([FileUtils resolvePath:urls]); } -// DocumentInteractionController delegate -- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller { - _result(@"Finished"); -} +// VideoPicker delegate -- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller { - NSLog(@"Finished"); - return _viewController; -} - -- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application { +- (void) resolvePickVideo{ - NSLog(@"Starting to send this puppy to %@", application); + UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init]; + videoPicker.delegate = self; + videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext; + videoPicker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4]; + videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh; + + [self.viewController presentViewController:videoPicker animated:YES completion:nil]; } -- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application { +- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { - NSLog(@"We're done sending the document."); + NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; + [picker dismissViewControllerAnimated:YES completion:NULL]; + _result([videoURL path]); +} + +- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { + [picker dismissViewControllerAnimated:YES completion:NULL]; } @end diff --git a/ios/Classes/FileUtils.h b/ios/Classes/FileUtils.h new file mode 100644 index 0000000..c5dbd83 --- /dev/null +++ b/ios/Classes/FileUtils.h @@ -0,0 +1,13 @@ +// +// FileUtils.h +// Pods +// +// Created by Miguel Ruivo on 05/12/2018. +// +@interface FileUtils : NSObject ++ (NSString*) resolveType:(NSString*)type; ++ (NSString*) resolvePath:(NSArray *)urls; +@end + + + diff --git a/ios/Classes/FileUtils.m b/ios/Classes/FileUtils.m new file mode 100644 index 0000000..10576ab --- /dev/null +++ b/ios/Classes/FileUtils.m @@ -0,0 +1,35 @@ +// +// FileUtils.m +// file_picker +// +// Created by Miguel Ruivo on 05/12/2018. +// + +#import "FileUtils.h" + +@implementation FileUtils + ++ (NSString*) resolveType:(NSString*)type { + + if ([type isEqualToString:@"PDF"]) { + return @"com.adobe.pdf"; + } + else if ([type isEqualToString:@"ANY"]) { + return @"public.item"; + } else { + return nil; + } +} + + ++ (NSString*) resolvePath:(NSArray *)urls{ + NSString * uri; + + for (NSURL *url in urls) { + uri = (NSString *)[url path]; + } + + return uri; +} + +@end diff --git a/lib/file_picker.dart b/lib/file_picker.dart index 5840c41..16f0234 100644 --- a/lib/file_picker.dart +++ b/lib/file_picker.dart @@ -7,6 +7,7 @@ enum FileType { ANY, PDF, IMAGE, + VIDEO, CAPTURE, } @@ -23,12 +24,14 @@ class FilePicker { static Future getFilePath({FileType type = FileType.ANY}) async { switch (type) { - case FileType.PDF: - return _getPath('PDF'); case FileType.IMAGE: return _getImage(ImageSource.gallery); case FileType.CAPTURE: return _getImage(ImageSource.camera); + case FileType.PDF: + return _getPath('PDF'); + case FileType.VIDEO: + return _getPath('VIDEO'); default: return _getPath('ANY'); }