diff --git a/android/.idea/caches/build_file_checksums.ser b/android/.idea/caches/build_file_checksums.ser index 107229a..427aae0 100644 Binary files a/android/.idea/caches/build_file_checksums.ser and b/android/.idea/caches/build_file_checksums.ser differ diff --git a/android/.idea/modules.xml b/android/.idea/modules.xml index c734d99..9bbbabb 100644 --- a/android/.idea/modules.xml +++ b/android/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file 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 c99d5e1..f1e2c28 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 @@ -31,6 +31,7 @@ public class FilePickerPlugin implements MethodCallHandler { private static final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE; private static Result result; private static Registrar instance; + private static String fileType; /** Plugin registration. */ public static void registerWith(Registrar registrar) { @@ -41,6 +42,7 @@ public class FilePickerPlugin implements MethodCallHandler { instance.addActivityResultListener(new PluginRegistry.ActivityResultListener() { @Override public boolean onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { if (data != null) { @@ -88,47 +90,77 @@ public class FilePickerPlugin implements MethodCallHandler { return false; } }); + + instance.addRequestPermissionsResultListener(new PluginRegistry.RequestPermissionsResultListener() { + @Override + public boolean onRequestPermissionsResult(int requestCode, String[] strings, int[] grantResults) { + if (requestCode == 0 && grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + startFileExplorer(fileType); + return true; + } + return false; + } + }); } @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.equals("pickPDF")) { - this.result = result; - startFileExplorer(); - } else { + this.result = result; + fileType = resolveType(call.method); + + if(fileType == null){ result.notImplemented(); + } else { + startFileExplorer(fileType); } + } - private boolean checkPermission() { + private static boolean checkPermission() { Activity activity = instance.activity(); Log.i(TAG, "Checking permission: " + permission); return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(activity, permission); } - private void requestPermission() { + private static void requestPermission() { + Activity activity = instance.activity(); Log.i(TAG, "Requesting permission: " + permission); String[] perm = { permission }; ActivityCompat.requestPermissions(activity, perm, 0); } - private void startFileExplorer() { + private String resolveType(String type) { + + switch (type){ + case "PDF": + return "application/pdf"; + case "ANY": + return "*/*"; + default: + return null; + } + } + + + + + private static void startFileExplorer(String type) { Intent intent; if (checkPermission()) { if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){ intent = new Intent(Intent.ACTION_PICK); - }else{ - intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + } else { + intent = new Intent(Intent.ACTION_GET_CONTENT); } - intent.setType("application/pdf"); + intent.setType(type); intent.addCategory(Intent.CATEGORY_OPENABLE); instance.activity().startActivityForResult(intent, REQUEST_CODE); } else { requestPermission(); - startFileExplorer(); } } diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index b694a11..405f678 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -182,6 +182,7 @@ TargetAttributes = { 97C146ED1CF9000F007C117D = { CreatedOnToolsVersion = 7.3.1; + DevelopmentTeam = KJ6ARNKBG8; }; }; }; @@ -428,6 +429,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; + DEVELOPMENT_TEAM = KJ6ARNKBG8; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -439,7 +441,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExample; + PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExamples; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; @@ -451,6 +453,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; + DEVELOPMENT_TEAM = KJ6ARNKBG8; ENABLE_BITCODE = NO; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", @@ -462,7 +465,7 @@ "$(inherited)", "$(PROJECT_DIR)/Flutter", ); - PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExample; + PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExamples; PRODUCT_NAME = "$(TARGET_NAME)"; VERSIONING_SYSTEM = "apple-generic"; }; diff --git a/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/example/ios/Runner/Info.plist b/example/ios/Runner/Info.plist index 15b1edd..b841323 100644 --- a/example/ios/Runner/Info.plist +++ b/example/ios/Runner/Info.plist @@ -4,15 +4,8 @@ CFBundleDevelopmentRegion en - CFBundleExecutable $(EXECUTABLE_NAME) - NSCameraUsageDescription - Used to demonstrate image picker plugin - NSMicrophoneUsageDescription - Used to capture audio for image picker plugin - NSPhotoLibraryUsageDescription - Used to demonstrate image picker plugin CFBundleIdentifier $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion @@ -29,6 +22,12 @@ $(FLUTTER_BUILD_NUMBER) LSRequiresIPhoneOS + NSCameraUsageDescription + Used to demonstrate image picker plugin + NSMicrophoneUsageDescription + Used to capture audio for image picker plugin + NSPhotoLibraryUsageDescription + Used to demonstrate image picker plugin UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/example/lib/main.dart b/example/lib/main.dart index c65897b..44e5cd2 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -11,8 +11,8 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String _path = '...'; String _fileName = '...'; + String _path = '...'; FileType _pickingType; void _openFileExplorer() async { @@ -63,6 +63,10 @@ class _MyAppState extends State { new DropdownMenuItem( child: new Text('FROM PDF'), value: FileType.PDF, + ), + new DropdownMenuItem( + child: new Text('ANY'), + value: FileType.ANY, ) ], onChanged: (value) { @@ -85,7 +89,7 @@ class _MyAppState extends State { style: new TextStyle(fontWeight: FontWeight.bold), ), new Text( - _path, + _path ?? '...', textAlign: TextAlign.center, softWrap: true, textScaleFactor: 0.85, diff --git a/lib/file_picker.dart b/lib/file_picker.dart index 8987b37..5840c41 100644 --- a/lib/file_picker.dart +++ b/lib/file_picker.dart @@ -2,12 +2,18 @@ import 'dart:async'; import 'package:flutter/services.dart'; import 'package:image_picker/image_picker.dart'; -import 'package:meta/meta.dart'; + +enum FileType { + ANY, + PDF, + IMAGE, + CAPTURE, +} class FilePicker { static const MethodChannel _channel = const MethodChannel('file_picker'); - static Future get _getPDF async => await _channel.invokeMethod('pickPDF'); + static Future _getPath(String type) async => await _channel.invokeMethod(type); static Future _getImage(ImageSource type) async { var image = await ImagePicker.pickImage(source: type); @@ -15,21 +21,16 @@ class FilePicker { return image?.path; } - static Future getFilePath({@required FileType type}) async { + static Future getFilePath({FileType type = FileType.ANY}) async { switch (type) { case FileType.PDF: - return _getPDF; + return _getPath('PDF'); case FileType.IMAGE: return _getImage(ImageSource.gallery); case FileType.CAPTURE: return _getImage(ImageSource.camera); + default: + return _getPath('ANY'); } - return ''; } } - -enum FileType { - PDF, - IMAGE, - CAPTURE, -}