Merge pull request #129 from lcaprini/master

showOpenWithDialog on Android
This commit is contained in:
pwlin 2017-05-30 12:19:58 +02:00 committed by GitHub
commit ac5ba3ffb3
2 changed files with 31 additions and 15 deletions

View File

@ -54,6 +54,21 @@ Open a PDF document with the default PDF reader and optional callback object:
} }
); );
Open a system modal to open PDF document with one of the already installed app and optional callback object:
cordova.plugins.fileOpener2.showOpenWithDialog(
'/sdcard/Download/starwars.pdf', // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
'application/pdf',
{
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
console.log('file opened successfully');
}
}
);
Notes Notes
------ ------
@ -64,16 +79,6 @@ Notes
- If you are wondering what MIME-type should you pass as the second argument to `open` function, [here is a list of all known MIME-types](http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co) - If you are wondering what MIME-type should you pass as the second argument to `open` function, [here is a list of all known MIME-types](http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/conf/mime.types?view=co)
Additional iOS Functions
---
The following functions are available in iOS platform:
`.showOpenWithDialog(fileName, contentType, callbackContext)`
---
Same as `open` function, but this will show openWith dialog on iOS for sending files into another apps.
---
Additional Android Functions Additional Android Functions
--- ---
The following functions are available in Android platform: The following functions are available in Android platform:

View File

@ -58,7 +58,13 @@ public class FileOpener2 extends CordovaPlugin {
*/ */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("open")) { if (action.equals("open")) {
this._open(args.getString(0), args.getString(1), callbackContext); String fileUrl = args.getString(0);
String contentType = args.getString(1);
Boolean openWithDefault = true;
if(args.length() > 2){
openWithDefault = args.getBoolean(2);
}
this._open(fileUrl, contentType, openWithDefault, callbackContext);
} }
else if (action.equals("uninstall")) { else if (action.equals("uninstall")) {
this._uninstall(args.getString(0), callbackContext); this._uninstall(args.getString(0), callbackContext);
@ -84,7 +90,7 @@ public class FileOpener2 extends CordovaPlugin {
return true; return true;
} }
private void _open(String fileArg, String contentType, CallbackContext callbackContext) throws JSONException { private void _open(String fileArg, String contentType, Boolean openWithDefault, CallbackContext callbackContext) throws JSONException {
String fileName = ""; String fileName = "";
try { try {
CordovaResourceApi resourceApi = webView.getResourceApi(); CordovaResourceApi resourceApi = webView.getResourceApi();
@ -98,7 +104,7 @@ public class FileOpener2 extends CordovaPlugin {
try { try {
Uri path = Uri.fromFile(file); Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
if((Build.VERSION.SDK_INT >= 23 && !contentType.equals("application/vnd.android.package-archive")) || (Build.VERSION.SDK_INT == 24 && contentType.equals("application/vnd.android.package-archive"))) { if(Build.VERSION.SDK_INT >= 23 && !contentType.equals("application/vnd.android.package-archive")){
Context context = cordova.getActivity().getApplicationContext(); Context context = cordova.getActivity().getApplicationContext();
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file); path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file);
@ -121,8 +127,13 @@ public class FileOpener2 extends CordovaPlugin {
* @see * @see
* http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin * http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
*/ */
cordova.getActivity().startActivity(intent); if(openWithDefault){
//cordova.getActivity().startActivity(Intent.createChooser(intent,"Open File in...")); cordova.getActivity().startActivity(intent);
}
else{
cordova.getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
}
callbackContext.success(); callbackContext.success();
} catch (android.content.ActivityNotFoundException e) { } catch (android.content.ActivityNotFoundException e) {
JSONObject errorObj = new JSONObject(); JSONObject errorObj = new JSONObject();