0

I just wanna save an image base64 and open it on gallery. I don't want to get pictures from gallery or take pictures.

<img src="base64" />
<button>Save</button>

phonegap + angularjs

Thanks!

3 Answers3

3

Solution:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {

  var fileTransfer = new FileTransfer();
  var uri = encodeURI("http://www.example.com/image");
  var path = fileSystem.root.toURL() + "appName/example.jpg";

  fileTransfer.download(
    uri,
    path,
    function(entry) {
      refreshMedia.refresh(path); // Refresh the image gallery
    },
    function(error) {
      console.log(error.source);
      console.log(error.target);
      console.log(error.code);
    },
    false,
    {
      headers: {
        "Authorization": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
      }
    }
  );

});

I needed to create a plugin to refresh the image gallery, because when you save an image on android device, this image does not appears on gallery. This plugin updates the image gallery.

refreshMedia.refresh(path); // Refresh the image gallery

Plugin: https://github.com/guinatal/refreshgallery

0

CLI: cordova plugin add org.apache.cordova.camera

  document.addEventListener("deviceready",onDeviceReady,false);

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value
    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }

    $(document).on('click','#pic_parse',function(){
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
        encodingType: Camera.EncodingType.PNG,
        destinationType: destinationType.DATA_URL,
        sourceType: pictureSource.SAVEDPHOTOALBUM });
    });

    function onPhotoDataSuccess(imageData) { 
        alert(imageData);//here, imageDate is base64.Now, you can save base64.
    }

function onFail(message) {
  alert('Failed because: ' + message);
}

Hope, it will helps you!

san
  • 214
  • 1
  • 10
0

To download base64 image on cordova, use this plugin which scanned your image into gallery also.

https://github.com/agomezmoron/cordova-save-image-gallery

function onDeviceReady() {
    var params = {data: base64String, prefix: 'myPrefix_', format: 'JPG', quality: 80, mediaScanner: true};
    window.imageSaver.saveBase64Image(params,
        function (filePath) {
          console.log('File saved on ' + filePath);
        },
        function (msg) {
          console.error(msg);
        }
      );
}
Hiren Raiyani
  • 754
  • 2
  • 12
  • 28