2015-02-09 52 views
3

使用Cordova FileOpener2和其他許多插件,如Cordova File,Cordova FileTransfer ..我無法在Android中找到我的本地PDF位置。Cordova FileOpener Android - 無法找到本地文件位置

此文件位於www/offline-files/目錄中,當我在iOS中打開它(使用window.open(encodeURI('offline-files/myFile.pdf'), '_blank');)時,它工作正常! 如果我在Android中嘗試相同的事情,它不起作用。例如,我多次嘗試中的一種:

function getPath() 
{ 
    // Get local path for Cordova 
    var path = window.location.pathname; 
    path = path.substr(path, path.length - 10); 
    return 'file://' + path; 
} 

cordova.plugins.fileOpener2.open(
    getPath() + 'offline-files/myFile.pdf', 
    'application/pdf', 
    { 
     error: function (e) 
     { 
      console.log('Error status: ' + e.status + ' - Error message: ' + e.message); 
     }, 
     success: function() 
     { 
      console.log('file opened successfully'); 
     } 
    } 
); 

科爾多瓦返回我的日誌:"Error status: 9 - Error message: File not found"

還有一個奇怪的錯誤:科爾多瓦文件插件返回我沒有找到經典目錄的錯誤文件:

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory, 
    function (dir) 
    { 
     /** SOME CODE **/ 
    } 
); 

回答

1

您使用的是什麼版本的Cordova?在任何情況下,我都不會硬編碼文件路徑,而是從LocalFileSystem對象獲取它。

var fs; 

function fsSuccess(fileSystem) 
{ 
    fs = fileSystem; 
} 

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

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

然後通過訪問根路徑:

fs.root.toURL() + "yourfilename" 

希望這有助於!