Search code examples
ionic-frameworkionic2cameraback-button

how to navigate to a page by clicking back button on camera open in ionic2


I have a HomePage and in ionViewWillEnter() I have added camera code. So that when HomePage get trigger it opens the Camera first

I want to customize the back button behavior that when I press the back button, after opening the camera it should navigate to SecondPage,by default it is navigating to HomePage.

ionViewWillEnter() {
   this.captureVideo();
 }


 captureVideo() {
    platform.registerBackButtonAction(() => {
      this.navCtrl.push(SecondPage)
       });
    let options: CaptureVideoOptions = { limit: 1 };
    this.mediaCapture.captureVideo(options)
     .then((videodata: MediaFile[]) => {
      var i, path, len;
     for (i = 0, len = videodata.length; i < len; i += 1) {
      path = videodata[i].fullPath;

      }

      this.flag_play = false;
      this.flag_upload = false;


     this.file.resolveLocalFilesystemUrl(path).then((newUrl) => {
     alert(JSON.stringify(newUrl))
     let dirPath = newUrl.nativeURL;
     let dirPathSegments = dirPath.split('/')
     dirPathSegments.pop()
     dirPath = dirPathSegments.join('/')
     this.file.readAsArrayBuffer(dirPath, newUrl.name).then(async (buffer) 
        => {
        await this.upload(buffer, newUrl.name);

          })

        })
        })
     .then(() => {


       var videoFileName = 'video-name-here'; 

       this.videoEditor.createThumbnail(

        {
         fileUri:'abc',// this.videoId,
         outputFileName: videoFileName,
         atTime: 2,
         width: 320,
         height: 480,
         quality: 100
        }
       ).then(result => {



     this.result = result;
     this.base64.encodeFile(result).then((base64File) => {
      this.base64Thumbnail = base64File.replace("*;charset=utf-8", "jpg")

       }, err => {

       alert("Unable to create thumbnail")
      })

       })
       })

       }

Solution

  • The hardware back button works as default behaviour of cancel ...so to customize it put your specific code in the error...

      takePicture(){
        this.camera.getPicture({
           destinationType: this.camera.DestinationType.DATA_URL,
          targetWidth: 1000,
          targetHeight: 1000
         }).then((imageData) => {
      // imageData is a base64 encoded string
           this.base64Image = "data:image/jpeg;base64," + imageData;
        }, (err) => {
       this.navCtrl.pop();
    
       });
      }
      }