Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta #15

Merged
merged 17 commits into from
Dec 6, 2018
Merged

Beta #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
.dart_tool/

.packages
.vscode
.idea
.pub/
pubspec.lock
.vscode
.idea

build/
19 changes: 0 additions & 19 deletions .idea/libraries/Dart_SDK.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/libraries/Flutter_for_Android.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/runConfigurations/example_lib_main_dart.xml

This file was deleted.

45 changes: 0 additions & 45 deletions .idea/workspace.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.0.0

* **Version 1.0** release.
* Adds support for ANY and VIDEO files.
* Fixes an issue where permissions were recursively asked on Android.
* Fixes an issue where some paths from document files couldn't be loaded with Android 8.0.
* Updates README file to match changes.
* General refactor & cleanup.

## 0.1.6
* Replaces commons dependency with FilePath class on Android, to handle path resolution on different SDK.

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ File picker plugin alows you to use a native file explorer to load absolute file
First, add *file_picker* as a dependency in [your pubspec.yaml file](https://flutter.io/platform-plugins/).

```
file_picker: ^0.1.3
file_picker: ^1.0
```
## Android
Add `<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />` to your app `AndroidManifest.xml` file.
Expand All @@ -20,11 +20,13 @@ Since we are using *image_picker* as a dependency from this plugin to load paths
* `NSMicrophoneUsageDescription` - describe why your app needs access to the microphone, if you intend to record videos. This is called _Privacy - Microphone Usage Description_ in the visual editor.

## To-do
* [X] Load paths from local files & cloud (GDrive, Dropbox, iCloud)
* [X] Load PDF file path
* [X] Load path from gallery
* [X] Load path from camera shot
* [ ] Load a custom format
* [X] Load paths from **cloud files** (GDrive, Dropbox, iCloud)
* [X] Load path from **PDF**
* [X] Load path from **gallery**
* [X] Load path from **camera**
* [X] Load path from **video**
* [X] Load path from **any** type of file (without filtering)
* [ ] Load path from a **custom format**

## Demo App

Expand All @@ -44,7 +46,7 @@ class _MyHomePageState extends State<MyHomePage> {

void getFilePath() async {
try {
String filePath = await FilePicker.getFilePath(type: FileType.PDF);
String filePath = await FilePicker.getFilePath(type: FileType.ANY);
if (filePath == '') {
return;
}
Expand Down
Binary file modified android/.idea/caches/build_file_checksums.ser
Binary file not shown.
1 change: 1 addition & 0 deletions android/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -41,12 +42,13 @@ public static void registerWith(Registrar registrar) {
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) {
Uri uri = data.getData();
Log.i(TAG, "URI:" +data.getData().toString());
String fullPath = FilePath.getPath(uri, instance.context());
String fullPath = FileUtils.getPath(uri, instance.context());
String cloudFile = null;

if(fullPath == null)
Expand Down Expand Up @@ -88,47 +90,79 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
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 "VIDEO":
return "video/*";
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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

/**
* Credits to NiRRaNjAN from package in.gauriinfotech.commons;.
**/
public class FilePath
public class FileUtils
{

private static final String tag = "FilePathPicker";
Expand Down Expand Up @@ -56,16 +57,22 @@ private static String getForApi19(Context context, Uri uri)
Log.e(tag, "+++ Primary External Document URI");
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
} else if (isDownloadsDocument(uri))
{
} else if (isDownloadsDocument(uri)) {
Log.e(tag, "+++ Downloads External Document URI");
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri))
{
if (!TextUtils.isEmpty(id)) {
if (id.startsWith("raw:")) {
return id.replaceFirst("raw:", "");
}
try {
final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} catch (Exception e) {
Log.e(tag, "+++ Something went wrong while retrieving document path: " + e.toString());
}
}
} else if (isMediaDocument(uri)) {
Log.e(tag, "+++ Media Document URI");
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
Expand Down
Binary file modified example/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
TargetAttributes = {
97C146ED1CF9000F007C117D = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = KJ6ARNKBG8;
};
};
};
Expand Down Expand Up @@ -269,7 +270,7 @@
};
AB4C7D1508951531E70F0A36 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
buildActionMask = 8;
files = (
);
inputPaths = (
Expand All @@ -280,7 +281,7 @@
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
);
runOnlyForDeploymentPostprocessing = 0;
runOnlyForDeploymentPostprocessing = 1;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
Expand Down Expand Up @@ -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)",
Expand All @@ -439,7 +441,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExample;
PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filepickerdemo;
PRODUCT_NAME = "$(TARGET_NAME)";
VERSIONING_SYSTEM = "apple-generic";
};
Expand All @@ -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)",
Expand All @@ -462,7 +465,7 @@
"$(inherited)",
"$(PROJECT_DIR)/Flutter",
);
PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filePickerExample;
PRODUCT_BUNDLE_IDENTIFIER = com.mr.flutter.plugin.filepickerdemo;
PRODUCT_NAME = "$(TARGET_NAME)";
VERSIONING_SYSTEM = "apple-generic";
};
Expand Down
Loading