PhoneGap open file in native app. Build via build.

2019-07-19 07:03发布

At first:

YES, there are many solutions in StackOverflow, but non of them works in my case.

  1. I got application built in SmartGWT.mobile
  2. I attached config files and all needed files to this build to prepare it for PhoneGap
  3. Application is build via build.phonegap.com site.
  4. It works perfectly fine on Android 4.1.1

I want to:

  1. Download file to local filesystem it is an PDF file - It is working fine using:

    var fileTransfer = new FileTransfer();
    fileTransfer.download(.....
    
  2. Open PDF in native app (for eg. Adobe Reader) whichever is installed on android for PDFs - it is not working:

    I tried:

    (1)

    cordova.exec("ChildBrowserCommand.showWebPage", encodeURI(theFile.toURL()) );
    

    (2)

    window.plugins.childBrowser.showWebPage(encodeURI(theFile.toURL()));
    

    (3)

    window.open(encodeURI(theFile.toURL()), '_blank', 'location=yes');
    

    (4) even HTML5 plugin for open PDFs by firefox

All variations with "file://" without with "./" at front and so on.

childBrowser shows only white screen, each time adds "http://" at front, window.open - the same.

I finally found something interesting like WebIntent, so i did:

    window.plugins.webintent.startActivity({
          action: window.plugins.webintent.ACTION_VIEW,
          type: "application/pdf",
          url: encodeURI(theFile.toURL().substring(7))},
          function() {},
          function() {alert('Failed to open URL via Android Intent')}
    );

but its not working due to fact that phonegap-build not attaching class file and It can not find WebIntent Class

I declare this plugin using in config.xml:

    <gap:plugin name="com.borismus.webintent.WebIntent" />

Do you know why it is not working, or what I'm doing worng ? Maybe you know other way to open file just like that in native app, it suppose to be simple

I just want my app to download and show (in native app) the PDF for user.

2条回答
孤傲高冷的网名
2楼-- · 2019-07-19 07:05

don's FileOpener version have worked on my app cordova 3.0

phonegap local plugin add https://github.com/don/FileOpener

all the xmls, plugin, etc are then added automatically.

added fileopener.js on index.html and then

window.plugins.fileOpener.open( path );
查看更多
Juvenile、少年°
3楼-- · 2019-07-19 07:19
$("#page").on('pageshow', function(event, ui) {
 if(event.handled !== true)
    {
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    event.handled = true;
    }
  return false;
});

function fail() {
    console.log("failed to get filesystem");
}

function gotFS(fileSystem) {
   console.log("got filesystem");

   // save the file system for later access
   console.log(fileSystem.root.fullPath);
   window.rootFS = fileSystem.root;
    downloadImage(url, fileName);
}

function downloadImage(url, fileName){
   var ft = new FileTransfer();
   ft.download(
    url,
    window.rootFS.fullPath + "/" + fileName,
    function(entry) {
        console.log("download complete: " + entry.fullPath);           
    },
    function(error) {
            console.log("download error" + error.code);
    }
 );
}
查看更多
登录 后发表回答