cordova使用fileOpener2打开刚下载好的APK时报there was a problem pasring the package

版权声明:爱我请告诉我 https://blog.csdn.net/hardly555/article/details/80545793

结果:
这里写图片描述

导致结果的原因:
当使用cordova-plugin-file-transfer下载apk时 使用的存储的路劲有问题
错误代码如下

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
             fs.root.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {  
              console.log(fileEntry);  
              //调用fileTransfer插件,下载文件  
              downLoadFile(fileEntry.toURL(),appurl); //!!!!!
          }, function(err) {  
             console.log(err);  
          });  
       }, function(error) {  
          console.log(error);  
       });

以上的代码构造的本地apk的url都会有错导致there was a problem pasring the package

修改:

var fileName=appurl.slice( appurl.lastIndexOf('/')+1)
        var targetPath = cordova.file.externalDataDirectory;//cordova.file.documentsDirectory;//'cdvfile://localhost/persistent/apk/';//注意!
        var filePath=targetPath+fileName
        downLoadFile(filePath,appurl);

成功,能成功打开下载的apk并顺利跳转安装页面

一下附上cordova下载服务器apk并打开安装的代码(js)

在初步探索过程中也遇到了几个问题以下附上参考解决的博文
谢谢
https://www.cnblogs.com/wupeng88/p/8567958.html(以帮助解决)
http://www.cnblogs.com/wupeng88/p/8533836.html(也遇到了此问题,但是我是删除安卓平台再添加,解决cordova platform rm android cordova platform add android –save)

function load(appurl){
        var fileName=appurl.slice( appurl.lastIndexOf('/')+1)
        var targetPath = cordova.file.externalDataDirectory;//cordova.file.documentsDirectory;//'cdvfile://localhost/persistent/apk/';
        var filePath=targetPath+fileName
        downLoadFile(filePath,appurl);

    //   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
    //       fs.root.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {  
    //           console.log(fileEntry);  
    //           //调用fileTransfer插件,下载文件  
    //           downLoadFile(fileEntry.toURL(),appurl); 
    //       }, function(err) {  
    //          console.log(err);  
    //       });  
    //    }, function(error) {  
    //       console.log(error);  
    //    });

    }

    function downLoadFile(filePath,remoteURL){
        // 服务器下载地址  
        var uri = encodeURI(remoteURL);  
        var fileTransfer = new FileTransfer();
       // var fileURL = fileEntry.toURL();
        var progress = window.navigator.dialogsPlus.progressStart("更新","获取中....");
        fileTransfer.download(
            uri,
            filePath,
            function(entry){
                //下载成功回调
                //打开下载完成的文件
                //progress.hide();
                    cordova.plugins.fileOpener2.open(
                    entry.toURL(), 
                    'application/vnd.android.package-archive',
                    {

                            error : function(e){ alert('失败status:'+JSON.stringify(e)+ " 路径:"+entry.toURL() )}, 
                            success : function(){  } 

                    });
            },
            function(error){
                //下载失败回调
                //下载失败的提示
                progress.hide();
                alert(JSON.stringify(error))
            },
            null,
            {}
        )
        fileTransfer.onprogress=function(progressEvent){
            if (progressEvent.lengthComputable) {
                progress.setValue((progressEvent.loaded/progressEvent.total)*100);
                if((progressEvent.loaded/progressEvent.total)==1){
                    progress.hide();
                }
            }
            else{
                alert(progressEvent.loaded+":"+progressEvent.total)
            }
            //alert(JSON.stringify(progressEvent))
        }

    }

猜你喜欢

转载自blog.csdn.net/hardly555/article/details/80545793