Open a file within the app folder with fileOpener Ionic2

自作多情 提交于 2019-12-11 04:25:40

问题


this is my code :

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FileOpener } from 'ionic-native';

@Component({
  selector: 'page-installHelper',
  templateUrl: 'installHelper.html'
})
export class InstallHelper {

  constructor(public navCtrl: NavController) {
              FileOpener.open('assets/app.apk', 'application/vnd.android.package-archive').then(
      function(){

          console.log("success");

          }, function(err){

              console.log("status : "+err.status);
              console.log("error : "+err.message);

          });
  }

}

but I can't access the file app.apk which is in assets/app.apk

and I get the error :

Status : 9
Error : File Not Found

is it even possible to access a file within the app folders ?


回答1:


Well I did it by making the app get downloaded from a server to a local folder I created in the phone and install it immediately/automatically,

Here is the code in case someone else needed it one day :

import { Component } from '@angular/core';
import { Platform, LoadingController } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { FileOpener } from 'ionic-native';
import { File } from 'ionic-native';
import { Transfer } from 'ionic-native';
import { HomePage } from '../pages/home/home';
declare var cordova: any;


@Component({
  template: `<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
  rootPage = HomePage;

  constructor(platform: Platform, public loadingCtrl: LoadingController) {
      let me = this;
    platform.ready().then(() => {

      let loading = me.loadingCtrl.create({
        content: 'Preparing The App ...'
      });
      loading.present();
      File.createDir(cordova.file.externalDataDirectory, "appFolder", true).then(function(link){ 

          const fileTransfer = new Transfer();
          let url = 'http://yourserverhere.com/app.apk';
          fileTransfer.download(url, cordova.file.externalDataDirectory +"appFolder/app.apk").then((entry) => {
            loading.dismiss();
            FileOpener.open(entry.toURL(), "application/vnd.android.package-archive").then(
              function(){
                 console.log("success");
              },function(err){
                  console.log("status : "+err.status);
                  console.log("error : "+err.message);
              });
          }, (error) => {
            console.log(error);
          });

      },function(error){
          console.log(error);
      });


      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }
}

Any explanation just ask me.




回答2:


Well since you want the user to download the .apk file, you could use (in your html)

<a href="assets/app.apk" download>Download apk</a>

But the user will have to manually open his downloads (or tap the popup) to install your app.

I do not know if there is a plugin which is capable of installing these apk files. (Googling for ionic 2 install external apk didn't return any results).



来源:https://stackoverflow.com/questions/40891028/open-a-file-within-the-app-folder-with-fileopener-ionic2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!