Ionic native camera.getPicture(options) always returns a string

Viewed 3408

I am using the getPicture function from @ionic-native/camera to get the file URI of the image. I have the cordova camera plugin and all the packages are updated. According to the documentation the default destination type option is File_URI. However, even when I explicitly specify my options with my default destination type as File_URI it returns a base64 string.

The source code is given below, am I missing something? Any help is appreciated.

import { Camera, CameraOptions } from '@ionic-native/camera';

    openGallery(){
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
    }

    this.camera.getPicture(options).then((imageURI) => {
     // imageData is either a base64 encoded string or a file URI
     // If it's base64 (DATA_URL):

     this.image_loc = imageURI;

     console.log("The image location is as follows: " + this.image_loc);

    }, (err) => {
     // Handle error
    }); 
}

Output in console:

enter image description here

2 Answers

try this

 base64Image:any;

      optionsCamera: CameraOptions = {
        quality: 100,
        destinationType: this.camera.DestinationType.FILE_URI,
        encodingType: this.camera.EncodingType.PNG,
        cameraDirection: this.camera.Direction.BACK,
        targetHeight:400,
        targetWidth: 400,
        correctOrientation: true,
        allowEdit: true

      }


    this.camera.getPicture(options).then((imageData) => {

          this.base64Image = imageData; 
          this.convertToUrl(imageData);

        }, (err) => {
          // console.log(err);

        });


    convertToUrl(newImage){
        function toDataURL(ctx, callback) {
          var url = ctx.base64Image;

          var xhr = new XMLHttpRequest();
          xhr.onload = function() {
            var reader = new FileReader();
            reader.onloadend = function() {
              ctx.base64Image = reader.result;
              callback(reader.result);       
            }
            reader.readAsDataURL(xhr.response);

          };
          xhr.open('GET', url);
          xhr.responseType = 'blob';
          xhr.send();
        }

        toDataURL(this, function(dataUrl) {
         console.log(dataUrl)
        })


      }

Try this Code:

  const options: CameraOptions = {
          quality: 80,
          destinationType: this.camera.DestinationType.FILE_URI,
          encodingType: this.camera.EncodingType.JPEG,
          mediaType: this.camera.MediaType.PICTURE,
          sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
        }
 this.camera.getPicture(options).then((imageData) => {
         // imageData is either a base64 encoded string or a file URI
         // If it's base64 (DATA_URL):
         console.log(imageData);
        }, (err) => {
         // Handle error
        });
Related