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

Added a "create" method, to allow for Save As/New File dialogs #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ Screenshot

![Screenshot](filechooser.png "Screenshot")



fileChooser.create(successCallback, failureCallback);

The success callback will receive the uri of the *created* file.

![Screenshot](filecreate.png "Screenshot")


NOTES:
- this file *is* created already, once your callback is triggered.
- the returned URI is a funky content://XYZ/%2Fencoded%2FURI%2FtoFile

WARNINGS:
- when you want to write a file, you will have to delete or overwrite the created file, should double check that it is indeed a file (and not a directory) and that it is indeed empty before overwriting (or prompt the user before continue, etc).


TODO rename `open` to pick, select, or choose.
TODO allow users to pass in suggested file name and mime-type to `create`.

Binary file added filecreate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion src/android/FileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class FileChooser extends CordovaPlugin {

private static final String TAG = "FileChooser";
private static final String ACTION_OPEN = "open";
private static final String ACTION_CREATE = "create";
private static final int PICK_FILE_REQUEST = 1;
private static final int CREATE_FILE_REQUEST = 2;
CallbackContext callback;

@Override
Expand All @@ -26,6 +28,11 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
return true;
}

if (action.equals(ACTION_CREATE)) {
chooseFileToCreate(callbackContext);
return true;
}

return false;
}

Expand All @@ -47,10 +54,45 @@ public void chooseFile(CallbackContext callbackContext) {
callbackContext.sendPluginResult(pluginResult);
}

/*
* File "creation" version of the "open" method, by Pat Deegan, https://psychogenic.com
*
* This android-native dialog allows you to move around the filesystem, create folders,
* and select or enter a filename for creation.
*
* NOTE 1: this file _is_ created. For instance, I've been using this with the File plugin/lib
* so I run filechooser create(), then use the cordova-plugin-file to _overwrite_ the contents of that
* file with my data.
*
* NOTE 2: the returned URI is a funky content://XYZ/%2Fencoded%2FURI%2FtoFile and I had to muck about
* to make it usable in my cordova app.
*
* WARNING: if the user in in some directory and hits save without entering a filename, you get the content
* URI back, but it points to a _directory_ so... caveat emptor.
*/
// TODO: would be nice to pass in: suggested file name, mime-type.
public void chooseFileToCreate(CallbackContext callbackContext) {
Intent intent = new Intent().setAction(Intent.ACTION_CREATE_DOCUMENT).setType("*/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);

Intent chooser = Intent.createChooser(intent, "Select file to create");


cordova.startActivityForResult(this, chooser, CREATE_FILE_REQUEST);


PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
callback = callbackContext;
callbackContext.sendPluginResult(pluginResult);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == PICK_FILE_REQUEST && callback != null) {

if ((requestCode == CREATE_FILE_REQUEST || requestCode == PICK_FILE_REQUEST ) && callback != null) {

if (resultCode == Activity.RESULT_OK) {

Expand Down
3 changes: 3 additions & 0 deletions www/fileChooser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = {
open: function (success, failure) {
cordova.exec(success, failure, "FileChooser", "open", []);
},
create: function (success, failure) {
cordova.exec(success, failure, "FileChooser", "create", []);
}
};