7

Recently I am building an application for android. I am using phonegap to do the same. Everything is running fine except one problem i.e. I cant open a pdf file using jquery in android. I have tried a lot to do the same but I am unable to do that.

What i am wanting is that after clicking on a image it will open a pdf from a url.

Edited:

I have tried this:

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

openPdf function is like that:

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

And fileOpener plugin is like that:

cordova.define("cordova/plugin/fileopener",
    function(require, exports, module) {
        var exec = require("cordova/exec");
        var FileOpener = function() {};

        FileOpener.prototype.open = function(url) {
            exec(null, null, "FileOpener", "openFile", [url]);
        };

        var 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");
}

I have add the plugin like in config.xml page like this:

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

And i have added FileOpener.java in the com.phonegap.plugins.file package like this:

    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 args, CallbackContext callbackContext) 
               {
        PluginResult.Status status = PluginResult.Status.OK;
        String result = "";

        try {
            if (action.equals("openFile")) {
                openFile(args.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.parse(url);

        Intent intent = null;
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type, 
        // so Android knew what application to use to open the file
        if (url.contains(YOU_TUBE)) {
            // If we don't do it this way you don't have the option for youtube
            uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v"));
            if (isYouTubeInstalled()) {
                intent = new Intent(Intent.ACTION_VIEW, uri);
            } else {
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://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.parse("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().startActivity(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;
        }
    }
   }

And i have added permission to the manifest file as well.

now using my fileopener plugin everything is opening perfect after changing file.java page.(You can see in the edited section). video, mp3, img is opening perfectly if it is stored localy or globaly. If pdf file is store in android/asset folder then it is showing me but if it is coming from website then it is showing me error.

Logcat is saying me:

  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

So can anyone help me what I am doing wrong? Thanks in advance.

Kauê Gimenes
  • 1,278
  • 1
  • 13
  • 30
Avijit
  • 3,834
  • 4
  • 33
  • 45
  • I hope the comma in window.plugins.fileOpener,open(pdfUrl); is only a typo?? Also, do you have adobe reader in your device? Have you tried opening a normal pdf file in the device? – SHANK Jul 26 '13 at 05:24
  • @SHANK Yap, I have abode reader im my device and I can see normal pdf in the device. And when i am using window.plugins.fileOpener.open(pdfUrl); Logcat is saying: 07-26 11:03:44.516: E/Web Console(2525): Uncaught TypeError: Cannot call method 'open' of undefined at file:///android_asset/www/index.html:60 – Avijit Jul 26 '13 at 05:34
  • Also, in your fileOpener plugin code, there is this comma issue FileOpener.prototype,open = function(url) {... I can see the case issue, you've used fileOpener , FileOpener, fileopener. It could be that mismatch as well – SHANK Jul 26 '13 at 06:05
  • @SHANK: I have edited the "." and case issue but now also it is not coming. Now in my browser i can see it is calling the function. In android device neither it is opening any image, video, pdf file nor showing me any error. – Avijit Jul 26 '13 at 06:59
  • I don't have a direct answer to it but try with debugging and post the line at which the execution stops. – SHANK Jul 26 '13 at 07:03
  • @SHANK now using my fileopener plugin everything is opening perfect after changing file.java page.(You can see in the edited section). video, mp3, img is opening perfectly if it is stored localy or globaly. If pdf file is store in android/asset folder then it is showing me but if it is coming from website then it is showing me error.(You can see error in edited section). – Avijit Jul 26 '13 at 09:33
  • please read this(https://developer.mozilla.org/en/docs/NPObject) and this(http://stackoverflow.com/questions/8936795/uncaught-error-error-calling-method-on-npobject). According to the answer on SO, it may be a cross-domain error. – SHANK Jul 26 '13 at 09:48
  • Correct me if I am wrong, but doesn't the FileOpener plug-in only work with local files stored on your Android device? It certainly says so in the plug-in description and all examples there are with file:///sdcard/.... – silverchair Jul 30 '13 at 08:04
  • Yah its correct, it is opening perfectly if i am using the local path. But when i am using web url it is not opening. But i am only facing this problem for pdf file. Can you tell me how to change the plugin so that it will open from web url? @silverchair – Avijit Jul 31 '13 at 05:08
  • can u please help me how you have used this plugin to open video file i am facing difficulty to open video file from asssets/www/video/videoFile.mp4 and run this file – nida Sep 26 '13 at 05:38
  • post a new question with the error you are getting and give me the link here @user1668447 – Avijit Sep 26 '13 at 06:10
  • please see the issue : http://stackoverflow.com/questions/19021246/undefined-result-using-fileopener-phonegap-android-and-failed-to-open-video-fi – nida Sep 26 '13 at 06:47
  • A different tact, if you are willing to allow the device to handle the viewing of the file: Use the InAppBrowser plugin (now called `cordova-plugin-inappbrowser`), and then use `window.open(pdfUrl, '_system');` – kris Jun 25 '15 at 02:27

3 Answers3

1

Disclaimer: I'm a developer on the PSPDFKit team

You could have a look at https://github.com/PSPDFKit/Cordova-Android and https://github.com/PSPDFKit/Cordova-iOS. Both plugins are wrapping PSPDFKit and allow viewing PDF documents as simple as

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

File downloading can be either done by a Cordova downloader plugin (as mentioned by Joram Teusink) or by simply extending the PSPDFKit plugin itself. We welcome every contribution to the project!

Community
  • 1
  • 1
david.schreiber
  • 3,851
  • 2
  • 28
  • 46
0

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

Create an extra function (method) that sends an PDF intent to Android. How to render PDF in 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);

Community
  • 1
  • 1
0

A different tact, if you are willing to allow the device to handle the viewing of the file: Use the InAppBrowser plugin (now called cordova-plugin-inappbrowser), and then use window.open(pdfUrl, '_system');

kris
  • 11,868
  • 9
  • 88
  • 110