adds support for any type of file;
fixes permission ask recursive loop;
This commit is contained in:
parent
3edcdf39f8
commit
2aa8ba18f5
Binary file not shown.
|
@ -3,6 +3,7 @@
|
|||
<component name="ProjectModuleManager">
|
||||
<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" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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";
|
||||
};
|
||||
|
|
|
@ -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>
|
|
@ -4,15 +4,8 @@
|
|||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
|
||||
<key>CFBundleExecutable</key>
|
||||
<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>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
|
@ -29,6 +22,12 @@
|
|||
<string>$(FLUTTER_BUILD_NUMBER)</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<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>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIMainStoryboardFile</key>
|
||||
|
|
|
@ -11,8 +11,8 @@ class MyApp extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String _path = '...';
|
||||
String _fileName = '...';
|
||||
String _path = '...';
|
||||
FileType _pickingType;
|
||||
|
||||
void _openFileExplorer() async {
|
||||
|
@ -63,6 +63,10 @@ class _MyAppState extends State<MyApp> {
|
|||
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<MyApp> {
|
|||
style: new TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
new Text(
|
||||
_path,
|
||||
_path ?? '...',
|
||||
textAlign: TextAlign.center,
|
||||
softWrap: true,
|
||||
textScaleFactor: 0.85,
|
||||
|
|
|
@ -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<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 {
|
||||
var image = await ImagePicker.pickImage(source: type);
|
||||
|
@ -15,21 +21,16 @@ class FilePicker {
|
|||
return image?.path;
|
||||
}
|
||||
|
||||
static Future<String> getFilePath({@required FileType type}) async {
|
||||
static Future<String> 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,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue