Skip to content
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
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@ Simple plugin which allows you to open popular files (PDF, WORD, EXCEL, JPG, GIF

Adding the Plugin to your project
-----------
Using this plugin requires Android PhoneGap. It has been successfully tested on Android device with Cordova 2.2

To install the plugin, move ``````fileopener.js`````` to your project's www folder and include a reference to it in your html file after ``````cordova-2.2.0.js``````.

```````html
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
<script type="text/javascript" charset="utf-8" src="fileopener.js"></script>
```````

Create a directory within your project called ``````src/com/phonegap/plugins/fileopener`````` and move ``````FileOpener.java`````` into it.

In your ``````res/xml/plugins.xml`````` file add the following line:

``````java
<plugin name="FileOpener" value="com.phonegap.plugins.fileopener.FileOpener"/>
``````
$ cordova plugin add https://github.com/don/FileOpener.git

Using the plugin
===========
Expand All @@ -40,6 +25,12 @@ After you run the command above, Android device will either open the file with p

RELEASE NOTES
-------------
Aug 1, 2013
Update for Cordova-3.0

May 21, 2013
Update for Cordova-2.7.0

January 2, 2013
Initial release

Expand Down
28 changes: 0 additions & 28 deletions fileopener.js

This file was deleted.

37 changes: 37 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin
xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.phonegap.plugins.fileopener"
version="0.0.3">

<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>

<name>File Opener</name>

<platform name="android">

<js-module src="www/fileopener.js" name="FileOpener">
<clobbers target="window.plugins.fileOpener" />
</js-module>

<config-file target="res/xml/config.xml" parent="/*">
<feature name="FileOpener">
<param name="android-package" value="com.phonegap.plugins.fileopener.FileOpener"/>
</feature>

</config-file>
<source-file src="src/android/FileOpener.java" target-dir="src/com/phonegap/plugins/fileopener" />
</platform>

<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="FileOpener">
<param name="wp-package" value="FileOpener"/>
</feature>
</config-file>
<source-file src="src/wp/FileOpener.cs" />
</platform>
</plugin>
48 changes: 25 additions & 23 deletions FileOpener.java → src/android/FileOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,48 @@

package com.phonegap.plugins.fileopener;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;

import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.CordovaPlugin;

public class FileOpener extends Plugin {
public class FileOpener extends CordovaPlugin {

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

try {
if (action.equals("openFile")) {
openFile(args.getString(0));
callbackContext.success();
return true;
}
else {
status = PluginResult.Status.INVALID_ACTION;
}
return new PluginResult(status, result);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
} catch (IOException e) {
return new PluginResult(PluginResult.Status.IO_EXCEPTION);
e.printStackTrace();
callbackContext.error(e.getMessage());
} catch (RuntimeException e) { // KLUDGE for Activity Not Found
e.printStackTrace();
callbackContext.error(e.getMessage());
}
return false;
}

private void openFile(String url) throws IOException {
// Create URI
Uri uri = Uri.parse(url);

Intent intent = null;
Intent intent;
// Check what kind of file you are trying to open, by comparing the url with extensions.
// When the if condition is matched, plugin sets the correct intent (mime) type,
// so Android knew what application to use to open the file

if (url.contains(".doc") || url.contains(".docx")) {
// Word document
intent = new Intent(Intent.ACTION_VIEW);
Expand Down Expand Up @@ -94,20 +90,26 @@ private void openFile(String url) throws IOException {
// Video files
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
}
}

//if you want you can also define the intent type for any other file

//additionally use else clause below, to manage other unknown extensions
//in this case, Android will show all applications installed on the device
//so you can choose which application to use

// else {
// intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(uri, "*/*");
// }

else {
String mimeType = URLConnection.guessContentTypeFromName(url);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
intent.setDataAndType(uri, mimeType);
}

this.cordova.getActivity().startActivity(intent);
Intent fileChooser = Intent.createChooser(intent, "Open File");
this.cordova.getActivity().startActivity(fileChooser); // TODO handle ActivityNotFoundException
}

}
50 changes: 50 additions & 0 deletions src/wp/FileOpener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Sergey Grebnov. Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using WPCordovaClassLib.Cordova.Commands;

namespace WPCordovaClassLib.Cordova.Commands
{
public class FileOpener : BaseCommand
{
async public void openFile(string options)
{
var optStings = JSON.JsonHelper.Deserialize<string[]>(options);

string filePath = optStings[0].Replace('/', '\\').Substring(2);

try
{

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await local.GetFileAsync(filePath);

if (file == null)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "File not found: " + filePath));
return;
}

var success = await Windows.System.Launcher.LaunchFileAsync(file);

if (success)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
}
else
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Unable to start the app associated with the following file: " + filePath));
}
}
catch (Exception ex) {
this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message));
}
}
}
}
17 changes: 17 additions & 0 deletions www/fileopener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// forked from https://github.com/markeeftb/FileOpener

module.exports = {
open: function (url, opt_success, opt_failure) {
if (typeof opt_success == 'undefined') {
opt_success = function () {
console.log("success!");
}
}
if (typeof opt_failure == 'undefined') {
opt_failure = function (error) {
console.log(error);
}
}
cordova.exec(opt_success, opt_failure, "FileOpener", "openFile", [url]);
}
}