├── LICENSE.txt ├── README.md ├── filechooser.png ├── plugin.xml ├── src └── android │ └── FileChooser.java └── www └── fileChooser.js /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2013-2014 Don Coleman 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Cordova FileChooser Plugin 2 | 3 | Requires Cordova >= 2.8.0 4 | 5 | Install with Cordova CLI 6 | 7 | $ cordova plugin add http://github.com/don/cordova-filechooser.git 8 | 9 | Install with Plugman 10 | 11 | $ plugman --platform android --project /path/to/project \ 12 | --plugin http://github.com/don/cordova-filechooser.git 13 | 14 | API 15 | 16 | fileChooser.open(successCallback, failureCallback); 17 | 18 | The success callback get the uri of the selected file 19 | 20 | fileChooser.open(function(uri) { 21 | alert(uri); 22 | }); 23 | 24 | Screenshot 25 | 26 | ![Screenshot](filechooser.png "Screenshot") 27 | 28 | TODO rename `open` to pick, select, or choose. 29 | -------------------------------------------------------------------------------- /filechooser.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/don/cordova-filechooser/c97ffb70a339ed2fa20eec5d7a7344a14cf268fd/filechooser.png -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | File Chooser 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/android/FileChooser.java: -------------------------------------------------------------------------------- 1 | package com.megster.cordova; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.net.Uri; 6 | import android.util.Log; 7 | 8 | import org.apache.cordova.CordovaArgs; 9 | import org.apache.cordova.CallbackContext; 10 | import org.apache.cordova.CordovaPlugin; 11 | import org.apache.cordova.PluginResult; 12 | import org.json.JSONException; 13 | 14 | public class FileChooser extends CordovaPlugin { 15 | 16 | private static final String TAG = "FileChooser"; 17 | private static final String ACTION_OPEN = "open"; 18 | private static final int PICK_FILE_REQUEST = 1; 19 | CallbackContext callback; 20 | 21 | @Override 22 | public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException { 23 | 24 | if (action.equals(ACTION_OPEN)) { 25 | chooseFile(callbackContext); 26 | return true; 27 | } 28 | 29 | return false; 30 | } 31 | 32 | public void chooseFile(CallbackContext callbackContext) { 33 | 34 | // type and title should be configurable 35 | 36 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 37 | intent.setType("*/*"); 38 | intent.addCategory(Intent.CATEGORY_OPENABLE); 39 | intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 40 | 41 | Intent chooser = Intent.createChooser(intent, "Select File"); 42 | cordova.startActivityForResult(this, chooser, PICK_FILE_REQUEST); 43 | 44 | PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT); 45 | pluginResult.setKeepCallback(true); 46 | callback = callbackContext; 47 | callbackContext.sendPluginResult(pluginResult); 48 | } 49 | 50 | @Override 51 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 52 | 53 | if (requestCode == PICK_FILE_REQUEST && callback != null) { 54 | 55 | if (resultCode == Activity.RESULT_OK) { 56 | 57 | Uri uri = data.getData(); 58 | 59 | if (uri != null) { 60 | 61 | Log.w(TAG, uri.toString()); 62 | callback.success(uri.toString()); 63 | 64 | } else { 65 | 66 | callback.error("File uri was null"); 67 | 68 | } 69 | 70 | } else if (resultCode == Activity.RESULT_CANCELED) { 71 | 72 | // TODO NO_RESULT or error callback? 73 | PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT); 74 | callback.sendPluginResult(pluginResult); 75 | 76 | } else { 77 | 78 | callback.error(resultCode); 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /www/fileChooser.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | open: function (success, failure) { 3 | cordova.exec(success, failure, "FileChooser", "open", []); 4 | } 5 | }; 6 | --------------------------------------------------------------------------------