mirror of
https://gitee.com/shuto/cordova-plugin-file-opener2.git
synced 2025-01-31 12:02:49 +08:00
Merge pull request #129 from lcaprini/master
showOpenWithDialog on Android
This commit is contained in:
commit
ac5ba3ffb3
25
README.md
25
README.md
@ -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
|
||||
------
|
||||
|
||||
@ -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)
|
||||
|
||||
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
|
||||
---
|
||||
The following functions are available in Android platform:
|
||||
|
@ -58,7 +58,13 @@ public class FileOpener2 extends CordovaPlugin {
|
||||
*/
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
||||
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")) {
|
||||
this._uninstall(args.getString(0), callbackContext);
|
||||
@ -84,7 +90,7 @@ public class FileOpener2 extends CordovaPlugin {
|
||||
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 = "";
|
||||
try {
|
||||
CordovaResourceApi resourceApi = webView.getResourceApi();
|
||||
@ -98,7 +104,7 @@ public class FileOpener2 extends CordovaPlugin {
|
||||
try {
|
||||
Uri path = Uri.fromFile(file);
|
||||
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();
|
||||
path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".opener.provider", file);
|
||||
@ -121,8 +127,13 @@ public class FileOpener2 extends CordovaPlugin {
|
||||
* @see
|
||||
* http://stackoverflow.com/questions/14321376/open-an-activity-from-a-cordovaplugin
|
||||
*/
|
||||
if(openWithDefault){
|
||||
cordova.getActivity().startActivity(intent);
|
||||
//cordova.getActivity().startActivity(Intent.createChooser(intent,"Open File in..."));
|
||||
}
|
||||
else{
|
||||
cordova.getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
|
||||
}
|
||||
|
||||
callbackContext.success();
|
||||
} catch (android.content.ActivityNotFoundException e) {
|
||||
JSONObject errorObj = new JSONObject();
|
||||
|
Loading…
Reference in New Issue
Block a user