Search code examples
androidionic2fileopener2

Open a file within the app folder with fileOpener Ionic2


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 ?


Solution

  • 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.