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
62 changes: 38 additions & 24 deletions FileOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,52 @@

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));
if( openFile(args.getString(0)) ) {
callbackContext.success();
}
else {
callbackContext.error("Could not open file");
}
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 {
private boolean 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 +94,34 @@ 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);
try
{
this.cordova.getActivity().startActivity(intent);
return true;
}
catch (Exception e)
{
return false;
}
}

}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ The plugin creates the object ``````window.plugins.fileOpener``````. To use, ca
Sample use:
----------
```````javascript
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc");
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/manual.pdf");
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/presentation.ppt");
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/image.jpg");
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc", failureCallbackFunction);
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/manual.pdf", failureCallbackFunction);
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/presentation.ppt", failureCallbackFunction);
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/image.jpg", failureCallbackFunction);
```````

After you run the command above, Android device will either open the file with proper external application installed on your device or ask you which application to use, if you haven't set before which application to use to open the specific type of file. What is great, when you exit the external app, Android will return to your application.
Expand Down
13 changes: 11 additions & 2 deletions fileopener.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
function FileOpener() {
};

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

/**
Expand Down