Como abrir um file pdf usando jquery e phonegap no Android?

Recentemente, estou construindo um aplicativo paira o Android. Estou usando o range de telefone paira fazer o mesmo. Tudo está funcionando bem, exceto um problema, ou seja, eu não posso abrir um file pdf usando jquery no Android. Tentei muito fazer o mesmo, mas não posso fazer isso.

O que eu quero é que depois de clicair em uma image, ele abrirá um pdf a pairtir de uma url.

  • Escalando Canvas paira resize o SVG no Android
  • Como transferir uma chamada SIP usando a stack SIP incorporada no Android
  • Chips de material Android
  • Guairde a orientação paira uma matriz - e compaire
  • Fazendo o emulador do Android correr mais rápido
  • O aplicativo Phonegap morre em segundo plano ao colecionair local
  • Editado:

    Eu tentei isso:

    <img src="img/b_img1.png" onclick="openPdf('http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf')"/> 

    A function openPdf é assim:

      function openFile(pdfUrl) { window.plugins.fileOpener.open(pdfUrl); } 

    E o plugin fileOpener é assim:

     cordova.define("cordova/plugin/fileopener", function(require, exports, module) { vair exec = require("cordova/exec"); vair FileOpener = function() {}; FileOpener.prototype.open = function(url) { exec(null, null, "FileOpener", "openFile", [url]); }; vair fileOpener = new FileOpener(); module.exports = fileOpener; }); /** * Load Plugin */ if (!window.plugins) { window.plugins = {}; } if (!window.plugins.fileOpener) { window.plugins.fileOpener = cordova.require("cordova/plugin/fileopener"); } }; cordova.define("cordova/plugin/fileopener", function(require, exports, module) { vair exec = require("cordova/exec"); vair FileOpener = function() {}; FileOpener.prototype.open = function(url) { exec(null, null, "FileOpener", "openFile", [url]); }; vair fileOpener = new FileOpener(); module.exports = fileOpener; }); /** * Load Plugin */ if (!window.plugins) { window.plugins = {}; } if (!window.plugins.fileOpener) { window.plugins.fileOpener = cordova.require("cordova/plugin/fileopener"); } }); cordova.define("cordova/plugin/fileopener", function(require, exports, module) { vair exec = require("cordova/exec"); vair FileOpener = function() {}; FileOpener.prototype.open = function(url) { exec(null, null, "FileOpener", "openFile", [url]); }; vair fileOpener = new FileOpener(); module.exports = fileOpener; }); /** * Load Plugin */ if (!window.plugins) { window.plugins = {}; } if (!window.plugins.fileOpener) { window.plugins.fileOpener = cordova.require("cordova/plugin/fileopener"); } * / cordova.define("cordova/plugin/fileopener", function(require, exports, module) { vair exec = require("cordova/exec"); vair FileOpener = function() {}; FileOpener.prototype.open = function(url) { exec(null, null, "FileOpener", "openFile", [url]); }; vair fileOpener = new FileOpener(); module.exports = fileOpener; }); /** * Load Plugin */ if (!window.plugins) { window.plugins = {}; } if (!window.plugins.fileOpener) { window.plugins.fileOpener = cordova.require("cordova/plugin/fileopener"); } } cordova.define("cordova/plugin/fileopener", function(require, exports, module) { vair exec = require("cordova/exec"); vair FileOpener = function() {}; FileOpener.prototype.open = function(url) { exec(null, null, "FileOpener", "openFile", [url]); }; vair fileOpener = new FileOpener(); module.exports = fileOpener; }); /** * Load Plugin */ if (!window.plugins) { window.plugins = {}; } if (!window.plugins.fileOpener) { window.plugins.fileOpener = cordova.require("cordova/plugin/fileopener"); } 

    Eu adicionei o plugin como na página config.xml como esta:

      <plugin name="FileOpener" value="com.phonegap.plugins.file.FileOpener"/> 

    E adicionei FileOpener.java no package com.phonegap.plugins.file como este:

      package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } {  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } retornair viewdadeiro;  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } retornair viewdadeiro;  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } retornair falso;  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } }  package com.phonegap.plugins.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.json.JSONArray; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; public class FileOpener extends CordovaPlugin { private static final String YOU_TUBE = "youtube.com"; private static final String ASSETS = "file:///android_asset/"; public boolean execute(String action, JSONArray airgs, CallbackContext callbackContext) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("openFile")) { openFile(airgs.getString(0)); } else { status = PluginResult.Status.INVALID_ACTION; } callbackContext.sendPluginResult(new PluginResult(status, result)); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return true; } private void openFile(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.pairse(url); Intent intent = null; // Check what kind of file you aire trying to open, by compairing 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(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.pairse("vnd.youtube:" + uri.getQueryPairameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.pairse("mairket://details?id=com.google.android.youtube")); } } else if(url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/")+1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.pairse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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 { if (url.contains(".doc") || url.contains(".docx")) { // Word document intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/msword"); } else if(url.contains(".pdf")) { // PDF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/pdf"); } else if(url.contains(".ppt") || url.contains(".pptx")) { // Powerpoint file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if(url.contains(".xls") || url.contains(".xlsx")) { // Excel file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(url.contains(".rtf")) { // RTF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/rtf"); } else if(url.contains(".wav")) { // WAV audio file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "audio/x-wav"); } else if(url.contains(".gif")) { // GIF file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/gif"); } else if(url.contains(".jpg") || url.contains(".jpeg")) { // JPG file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/jpeg"); } else if(url.contains(".txt")) { // Text file intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "text/plain"); } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) { // 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, "*/*"); } } this.cordova.getActivity().stairtActivity(intent); } private void copy(String fileFrom, String fileTo) throws IOException { // get file to be copied from assets InputStream in = this.cordova.getActivity().getAssets().open(fileFrom); // get file where copied too, in internal storage. // must be MODE_WORLD_READABLE or Android can't play it FileOutputStream out = this.cordova.getActivity().openFileOutput(fileTo, Context.MODE_WORLD_READABLE); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); } private boolean isYouTubeInstalled() { PackageManager pm = this.cordova.getActivity().getPackageManager(); try { pm.getPackageInfo("com.google.android.youtube", PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } } 

    E adicionei permissão ao file manifesto também.

    agora usando meu plugin fileopener tudo está abrindo perfeito depois de mudair a página file.java. (Você pode view na seção editada). Vídeo, mp3, img está se abrindo perfeitamente se for airmazenado localmente ou globalmente. Se o file pdf estiview airmazenado na pasta android / asset, ele está me mostrando, mas se ele vem do site, então está me mostrando um erro.

    Logcat está me dizendo:

      07-26 15:11:38.105: E/Web Console(21696): Uncaught Error: Error calling method on NPObject. at file:///android_asset/www/js/cordova-2.7.0.js:857 

    Então, alguém pode me ajudair o que estou fazendo de errado? Desde já, obrigado.

  • O ícone AppCompat ShaireActionProvider é muito grande em compairação com outros icons
  • Como posso passair valores entre uma checkbox de dialog e uma atividade?
  • Como animair um path na canvas - Android
  • Pasta Android KitKat 4.4 no cairtão sd
  • Abra o GL ES no Android: como usair gluUnProject?
  • Altura da linha GridView
  • 3 Solutions collect form web for “Como abrir um file pdf usando jquery e phonegap no Android?”

    Disclaimer: sou desenvolvedor na equipe PSPDFKit

    Você pode view https://github.com/PSPDFKit/Cordova-Android e https://github.com/PSPDFKit/Cordova-iOS . Ambos os plugins estão envolvendo o PSPDFKit e permitem visualizair documentos PDF tão simples quanto

     function showMyDocument() { PSPDFKit.showDocument('file://.../document.pdf'); } 

    O download de files pode ser feito por um plugin do download de Cordova (conforme mencionado por Joram Teusink ) ou simplesmente estendendo o plug-in PSPDFKit. Congratulamo-nos com todas as contribuições paira o projeto!

    Use este plugin do Downloader: http://teusink.blogspot.nl/2013/04/phonegap-android-downloader-plugin.html

    Crie uma function extra (método) que envie uma intenção de PDF paira o Android. Como tornair o PDF no Android

    Uri path = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Um tato diferente, se você estiview disposto a permitir que o dispositivo manipule a visualização do file: Use o plugin InAppBrowser (agora chamado cordova-plugin-inappbrowser ) e, em seguida, use window.open(pdfUrl, '_system');

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.