Phonegap打开本地文件(pdf):"未找到"使用FileOpener插件

时间:2015-06-08 19:33:58

标签: android cordova phonegap-plugins phonegap-build

使用io.github.pwlin.cordova.plugins.fileopener2,我无法在Android中打开我的本地PDF文件。

在当地,我有一个" / pdf /"文件夹(1.pdf,...到6.pdf)。

这是我的PhoneGap config.xml

<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="app.example.com" version="1.0.0">
  <name><![CDATA[Example]]></name>
  <description>...</description>
  <author href="http://www.example.com" email="hello@example.com">Example</author>
  <content src="index.html"/>
  <preference name="permissions" value="none"/>
  <preference name="orientation" value="default"/>
  <preference name="target-device" value="universal"/>
  <preference name="fullscreen" value="true"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="prerendered-icon" value="true"/>
  <preference name="stay-in-webview" value="false"/>
  <preference name="ios-statusbarstyle" value="black-opaque"/>
  <preference name="detect-data-types" value="true"/>
  <preference name="exit-on-suspend" value="false"/>
  <preference name="show-splash-screen-spinner" value="true"/>
  <preference name="auto-hide-splash-screen" value="true"/>
  <preference name="disable-cursor" value="false"/>
  <preference name="android-minSdkVersion" value="7"/>
  <preference name="android-installLocation" value="auto"/>
  <preference name="useBrowserHistory" value="true" />
  <gap:plugin name="org.apache.cordova.console"/>
  <gap:plugin name="org.apache.cordova.device"/>
  <gap:plugin name="org.apache.cordova.dialogs"/>
  <gap:plugin name="org.apache.cordova.file"/>
  <gap:plugin name="org.apache.cordova.file-transfer"/>
  <gap:plugin name="org.apache.cordova.inappbrowser"/>
  <gap:plugin name="org.apache.cordova.network-information"/>
  <gap:plugin name="io.github.pwlin.cordova.plugins.fileopener2" version="1.0.11" />
  <icon src="icon.png"/>
  <icon src="res/icon/android/icon-36-ldpi.png" gap:platform="android" gap:qualifier="ldpi"/>
  <icon src="res/icon/android/icon-48-mdpi.png" gap:platform="android" gap:qualifier="mdpi"/>
  <icon src="res/icon/android/icon-72-hdpi.png" gap:platform="android" gap:qualifier="hdpi"/>
  <icon src="res/icon/android/icon-96-xhdpi.png" gap:platform="android" gap:qualifier="xhdpi"/>
  <icon src="res/icon/ios/icon-57.png" gap:platform="ios" width="57" height="57"/>
  <icon src="res/icon/ios/icon-72.png" gap:platform="ios" width="72" height="72"/>
  <icon src="res/icon/ios/icon-57-2x.png" gap:platform="ios" width="114" height="114"/>
  <icon src="res/icon/ios/icon-72-2x.png" gap:platform="ios" width="144" height="144"/>
  <gap:splash src="res/screen/android/screen-ldpi-portrait.png" gap:platform="android" gap:qualifier="port-ldpi"/>
  <gap:splash src="res/screen/android/screen-mdpi-portrait.png" gap:platform="android" gap:qualifier="port-mdpi"/>
  <gap:splash src="res/screen/android/screen-hdpi-portrait.png" gap:platform="android" gap:qualifier="port-hdpi"/>
  <gap:splash src="res/screen/android/screen-xhdpi-portrait.png" gap:platform="android" gap:qualifier="port-xhdpi"/>
  <gap:splash src="res/screen/ios/screen-iphone-portrait.png" gap:platform="ios" width="320" height="480"/>
  <gap:splash src="res/screen/ios/screen-iphone-portrait-2x.png" gap:platform="ios" width="640" height="960"/>
  <gap:splash src="res/screen/ios/screen-iphone-portrait-568h-2x.png" gap:platform="ios" width="640" height="1136"/>
  <gap:splash src="res/screen/ios/screen-ipad-portrait.png" gap:platform="ios" width="768" height="1024"/>
  <gap:splash src="res/screen/ios/screen-ipad-landscape.png" gap:platform="ios" width="1024" height="768"/>
  <access origin="*"/>
  <plugin name="cordova-plugin-whitelist" version="1"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <allow-intent href="tel:*"/>
  <allow-intent href="sms:*"/>
  <allow-intent href="mailto:*"/>
  <allow-intent href="geo:*"/>
  <platform name="android">
    <allow-intent href="market:*"/>
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*"/>
    <allow-intent href="itms-apps:*"/>
  </platform>
  <feature name="http://api.phonegap.com/1.0/file"/>
</widget>

我已经在我的html文件中尝试了很多类似的东西:

<!-- openPDF is a personnal function... see next block -->
<a onClick="openPDF('pdf/1.pdf');">Open PDF</a>

我的函数(js文件):

var fs;

function fsSuccess(fileSystem)
{
    fs = fileSystem;
}

function fsFail(event)
{
    console.log(event.target.error.code);
}

function openPDF(file) {

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);     

    file = fs.root.toURL() + file;

    cordova.plugins.fileOpener2.open(
    file, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
    'application/pdf', 
    { 
        error : function(e) { 
            console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
            $('#errorpdf').html('Error status: ' + e.status + ' - Error message: ' + e.message+' - File : '+file);
        },
        success : function () {
            console.log('file opened successfully');          
            $('#errorpdf').html('file opened successfully');

        }
    }
);
}

Cordova总是给我回复:&#34;错误状态:9 - 错误消息:找不到文件&#34;

我哪里错了?

编辑:我的文件夹 /storage/emulated/0/Android/data/com.example.app/files /storage/emulated/0/Android/data/com.example.app/cache 显然是空的,我不知道如何将本地.pdf文件移动到此文件夹。

1 个答案:

答案 0 :(得分:0)

最后,我这样做了,有2个插件:

  <gap:plugin name="io.github.pwlin.cordova.plugins.fileopener2" version="1.0.11" />
  <gap:plugin name="com.gkcgautam.asset2sd" version="2.0.0" />

这里我的整个代码没有清理(对不起),如果你需要它,你可以删除一些行,如视觉和#34;反馈&#34;等。

/* First of all... copy a directory to local storage */
function copyPDF(file) {

    var timeNow = Math.floor(Date.now() / 1000);
    var timeLastDataBaux = localStorage.getItem("bauxPDFDate");

    // Don't copy the PDF for 15 min., it will be faster
    if(timeLastDataBaux > (timeNow-900)) // (60 * 15 = 900) 15 minutes de cache
    {           
        if(localStorage.getItem("bauxPDFDate") !== null)
        {
            // Display buttons to open PDFs
            $('#baux_page_loading').fadeOut('slow', function() {
                $('#baux_page_loaded').fadeIn('fast');
            });
            return true;
        }
        else
        {
        // ?
            console.log('First copy of files...');
        }
    } 
    else 
    { 
    // Else, we have no data, or too old ? normal way => we launch the copy etc. 
        console.log('Files too old...');
    }


    asset2sd.copyDir({ 
          asset_directory: file, // 'www/pdf' 
          destination_directory: "MyDir" 
          },
          function(result) 
          {
            // SUCCESS ! 
            $('#baux_page_loading').html('<p>✓ Well loaded&nbsp;!</p>');

            var timestamp = Math.floor(Date.now() / 1000);
            localStorage.setItem('bauxPDFDate', timestamp);

            // Display button to open PDFs
            $('#baux_page_loading').fadeOut('slow', function() {
                $('#baux_page_loaded').fadeIn('fast');
            });

          }, function() 
          {
            // ERROR
            $('#baux_page_loading').fadeOut('normal');
            $('#errorpdf').html('<div class="error">Impossible to load PDF files on the internal storage space or SD card (erreur : '+file+' - '+result+'). Please see our website <a onclick="http://www.example.com" class="coloredTap">www.example.com</a>  to download its PDF.</div>');
          });  
}

/* Second, this is the way to open local file already copied with asset2sd */
function openPDF(filename, btn) {

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);

    file = fs.root.toURL() + filename;

    //$(btn).fadeOut('fast', function() { $(btn).fadeIn('fast'); });
    $(btn).after( '<img src="images/loader.gif" align="absmiddle" id="littleLoader">' );

    cordova.plugins.fileOpener2.open(
    file, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
    'application/pdf', 
        { 
            error : function(e) { 
                //console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                $('#errorpdf3').html('<div class="error">Cannot open PDF in this path (erreur : ' + e.status + ' - msg : ' + e.message+' - chemin : '+file+'). Please see our website <a onclick="http://www.example.com" class="coloredTap">www.example.com</a> to download it.</div>');
            },
            success : function () {
                //console.log('file opened successfully');  
                  setTimeout(function(){ 
                        $('#littleLoader').fadeOut('normal');
                        $('#littleLoader').remove();
                    }, 1500);
                //$('#errorpdf3').html('file opened successfully');

            }
        }
    ); 

}