adds support for any type of file;

fixes permission ask recursive loop;
This commit is contained in:
Miguel Ruivo 2018-11-30 16:15:09 +00:00
parent 3edcdf39f8
commit 2aa8ba18f5
8 changed files with 81 additions and 33 deletions

View File

@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/file_picker.iml" filepath="$PROJECT_DIR$/file_picker.iml" /> <module fileurl="file://$PROJECT_DIR$/file_picker.iml" filepath="$PROJECT_DIR$/file_picker.iml" />
<module fileurl="file://$PROJECT_DIR$/file_picker.iml" filepath="$PROJECT_DIR$/file_picker.iml" />
</modules> </modules>
</component> </component>
</project> </project>

View File

@ -31,6 +31,7 @@ public class FilePickerPlugin implements MethodCallHandler {
private static final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE; private static final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
private static Result result; private static Result result;
private static Registrar instance; private static Registrar instance;
private static String fileType;
/** Plugin registration. */ /** Plugin registration. */
public static void registerWith(Registrar registrar) { public static void registerWith(Registrar registrar) {
@ -41,6 +42,7 @@ public class FilePickerPlugin implements MethodCallHandler {
instance.addActivityResultListener(new PluginRegistry.ActivityResultListener() { instance.addActivityResultListener(new PluginRegistry.ActivityResultListener() {
@Override @Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) { public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) { if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) { if (data != null) {
@ -88,47 +90,77 @@ public class FilePickerPlugin implements MethodCallHandler {
return false; 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 @Override
public void onMethodCall(MethodCall call, Result result) { public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("pickPDF")) { this.result = result;
this.result = result; fileType = resolveType(call.method);
startFileExplorer();
} else { if(fileType == null){
result.notImplemented(); result.notImplemented();
} else {
startFileExplorer(fileType);
} }
} }
private boolean checkPermission() { private static boolean checkPermission() {
Activity activity = instance.activity(); Activity activity = instance.activity();
Log.i(TAG, "Checking permission: " + permission); Log.i(TAG, "Checking permission: " + permission);
return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(activity, permission); return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(activity, permission);
} }
private void requestPermission() { private static void requestPermission() {
Activity activity = instance.activity(); Activity activity = instance.activity();
Log.i(TAG, "Requesting permission: " + permission); Log.i(TAG, "Requesting permission: " + permission);
String[] perm = { permission }; String[] perm = { permission };
ActivityCompat.requestPermissions(activity, perm, 0); 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; Intent intent;
if (checkPermission()) { if (checkPermission()) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
intent = new Intent(Intent.ACTION_PICK); intent = new Intent(Intent.ACTION_PICK);
}else{ } else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent = new Intent(Intent.ACTION_GET_CONTENT);
} }
intent.setType("application/pdf"); intent.setType(type);
intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addCategory(Intent.CATEGORY_OPENABLE);
instance.activity().startActivityForResult(intent, REQUEST_CODE); instance.activity().startActivityForResult(intent, REQUEST_CODE);
} else { } else {
requestPermission(); requestPermission();
startFileExplorer();
} }
} }

View File

@ -182,6 +182,7 @@
TargetAttributes = { TargetAttributes = {
97C146ED1CF9000F007C117D = { 97C146ED1CF9000F007C117D = {
CreatedOnToolsVersion = 7.3.1; CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = KJ6ARNKBG8;
}; };
}; };
}; };
@ -428,6 +429,7 @@
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = KJ6ARNKBG8;
ENABLE_BITCODE = NO; ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = ( FRAMEWORK_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
@ -439,7 +441,7 @@
"$(inherited)", "$(inherited)",
"$(PROJECT_DIR)/Flutter", "$(PROJECT_DIR)/Flutter",
); );
PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExample; PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExamples;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
VERSIONING_SYSTEM = "apple-generic"; VERSIONING_SYSTEM = "apple-generic";
}; };
@ -451,6 +453,7 @@
buildSettings = { buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)"; CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
DEVELOPMENT_TEAM = KJ6ARNKBG8;
ENABLE_BITCODE = NO; ENABLE_BITCODE = NO;
FRAMEWORK_SEARCH_PATHS = ( FRAMEWORK_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
@ -462,7 +465,7 @@
"$(inherited)", "$(inherited)",
"$(PROJECT_DIR)/Flutter", "$(PROJECT_DIR)/Flutter",
); );
PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExample; PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExamples;
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
VERSIONING_SYSTEM = "apple-generic"; VERSIONING_SYSTEM = "apple-generic";
}; };

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@ -4,15 +4,8 @@
<dict> <dict>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>en</string> <string>en</string>
<key>CFBundleExecutable</key> <key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string> <string>$(EXECUTABLE_NAME)</string>
<key>NSCameraUsageDescription</key>
<string>Used to demonstrate image picker plugin</string>
<key>NSMicrophoneUsageDescription</key>
<string>Used to capture audio for image picker plugin</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to demonstrate image picker plugin</string>
<key>CFBundleIdentifier</key> <key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key> <key>CFBundleInfoDictionaryVersion</key>
@ -29,6 +22,12 @@
<string>$(FLUTTER_BUILD_NUMBER)</string> <string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>
<true/> <true/>
<key>NSCameraUsageDescription</key>
<string>Used to demonstrate image picker plugin</string>
<key>NSMicrophoneUsageDescription</key>
<string>Used to capture audio for image picker plugin</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to demonstrate image picker plugin</string>
<key>UILaunchStoryboardName</key> <key>UILaunchStoryboardName</key>
<string>LaunchScreen</string> <string>LaunchScreen</string>
<key>UIMainStoryboardFile</key> <key>UIMainStoryboardFile</key>

View File

@ -11,8 +11,8 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
String _path = '...';
String _fileName = '...'; String _fileName = '...';
String _path = '...';
FileType _pickingType; FileType _pickingType;
void _openFileExplorer() async { void _openFileExplorer() async {
@ -63,6 +63,10 @@ class _MyAppState extends State<MyApp> {
new DropdownMenuItem( new DropdownMenuItem(
child: new Text('FROM PDF'), child: new Text('FROM PDF'),
value: FileType.PDF, value: FileType.PDF,
),
new DropdownMenuItem(
child: new Text('ANY'),
value: FileType.ANY,
) )
], ],
onChanged: (value) { onChanged: (value) {
@ -85,7 +89,7 @@ class _MyAppState extends State<MyApp> {
style: new TextStyle(fontWeight: FontWeight.bold), style: new TextStyle(fontWeight: FontWeight.bold),
), ),
new Text( new Text(
_path, _path ?? '...',
textAlign: TextAlign.center, textAlign: TextAlign.center,
softWrap: true, softWrap: true,
textScaleFactor: 0.85, textScaleFactor: 0.85,

View File

@ -2,12 +2,18 @@ import 'dart:async';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:meta/meta.dart';
enum FileType {
ANY,
PDF,
IMAGE,
CAPTURE,
}
class FilePicker { class FilePicker {
static const MethodChannel _channel = const MethodChannel('file_picker'); static const MethodChannel _channel = const MethodChannel('file_picker');
static Future<String> get _getPDF async => await _channel.invokeMethod('pickPDF'); static Future<String> _getPath(String type) async => await _channel.invokeMethod(type);
static Future<String> _getImage(ImageSource type) async { static Future<String> _getImage(ImageSource type) async {
var image = await ImagePicker.pickImage(source: type); var image = await ImagePicker.pickImage(source: type);
@ -15,21 +21,16 @@ class FilePicker {
return image?.path; return image?.path;
} }
static Future<String> getFilePath({@required FileType type}) async { static Future<String> getFilePath({FileType type = FileType.ANY}) async {
switch (type) { switch (type) {
case FileType.PDF: case FileType.PDF:
return _getPDF; return _getPath('PDF');
case FileType.IMAGE: case FileType.IMAGE:
return _getImage(ImageSource.gallery); return _getImage(ImageSource.gallery);
case FileType.CAPTURE: case FileType.CAPTURE:
return _getImage(ImageSource.camera); return _getImage(ImageSource.camera);
default:
return _getPath('ANY');
} }
return '';
} }
} }
enum FileType {
PDF,
IMAGE,
CAPTURE,
}