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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ 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``````.
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.7.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="cordova-2.7.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:
In your ``````res/xml/config.xml`````` file add the following line:

``````java
<plugin name="FileOpener" value="com.phonegap.plugins.fileopener.FileOpener"/>
Expand All @@ -40,6 +40,9 @@ After you run the command above, Android device will either open the file with p

RELEASE NOTES
-------------
May 21, 2013
Update for Cordova-2.7.0

January 2, 2013
Initial release

Expand Down
21 changes: 21 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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.FileOpener"
version="0.0.1">

<name>FileOpener</name>

<asset src="www/fileopener.js" target="js/fileopener.js"/>

<platform name="android">

<config-file target="res/xml/config.xml" parent="/cordova/plugins">
<plugin name="FileOpener" value="com.phonegap.plugins.fileopener.FileOpener"/>
</config-file>

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

</platform>
</plugin>
46 changes: 24 additions & 22 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.api.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.api.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);
this.cordova.getActivity().startActivity(intent); // TODO handle ActivityNotFoundException
}

}
8 changes: 7 additions & 1 deletion fileopener.js → www/fileopener.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ function FileOpener() {
};

FileOpener.prototype.open = function(url) {
cordova.exec(null, null, "FileOpener", "openFile", [url]);
var success = function() {
console.log("success!");
}
var failure = function(error) {
console.log(error);
}
cordova.exec(success, failure, "FileOpener", "openFile", [url]);
};

/**
Expand Down