cordova项目使用cordova.plugins.fileOpener2打开文件目录,报错(解析包出错)

新手上路,请多包涵

我使用的是vue.js配合cordova的项目使用。跑的是测试版的项目,所以已经是默认签名的包了。在自己的华为手机上,使用的是‘file:///storage/emulated/0/path/to/1.apk’这个路径可以正常打开,安装。但是在其他手机上就没法了。报错:包解析错误。(怀疑是路径的问题)可以把包放在最外层的目录么?求大神告知怎么写的
var fileTransfer = new FileTransfer();

      var openUrl = '';
      var url = encodeURI(this.update.url); //app下载路径
      // var targetPath = 'file:///storage/emulated/0/path/to/9xlink.apk';
      var targetPath = 'cdvfile://localhost/persistent/9xlink-pda.apk';

      var options = {};
      var that = this;
      var trustHosts = true;
      if(this.update.url != undefined){
        fileTransfer.download(
            url,
            targetPath,
            function(entry) {
              openUrl = entry.toURL();
              console.log("download complete: " + openUrl);
              //下载完成,打开app
              cordova.plugins.fileOpener2.open(
                  openUrl, 
                  'application/vnd.android.package-archive',
                  {
                    error:function(e){
                      console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                    },
                    success:function(){
                      console.log('open successfully');
                    }
                  }
              );
            },
            function(error) {
              that.alert.message = '下载失败,请稍后再试';
              that.alert.status = 409;
              that.setal(that.alert);
              console.log("download error source " + error.source);
              console.log("download error target " + error.target);
              console.log("upload error code" + error.code);
            },
            true           
        );
        //下载进度
        fileTransfer.onprogress = function(progressEvent) {
            that.percent = progressEvent.loaded / progressEvent.total; //下载百分比
            that.perStyle.width = (that.percent*100)+'%';
            console.log(that.perStyle.width,that.percent);
            if(that.percent == 1){
              that.update.tf = false;                                
            }                          
        }
阅读 7.5k
2 个回答
✓ 已被采纳新手上路,请多包涵

问题已经解决了。我用的是这个路径‘cdvfile://localhost/persistent/xxx.apk’,并且在config.xml里面进行了配置。如下:
<preference name="iosPersistentFileLocation" value="Compatibility" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题