Search code examples
angularjscordovangcordova

Copy image file from /storage/emulated/0/Pictures to another directory in cordova


I am building a small hybrid app. I used the cordova-base64-to-gallery plugin to covert a base64 image from a cropped image (using Croppie) to a png file which is then stored in the Picture's folder, in the phone's image gallery. Saving the base64 string to an image works well so far and I can see it in the image gallery. But my problem is how to copy this image which returns the path as /storage/emulated/0/Pictures/img_imageFile.png to the cordova.file.dataDirectory using ngCordova file plugin.

Here is my angularjs code

// Beginning the conversion process...
cordova.base64ToGallery(base64ImageString, {
                                             prefix : 'img_',
                                             mediaScanner : true
                                           },

     function (path) {
         //path = /storage/emulated/0/Pictures/img_imageFile.png
         //console.log(path);
         var sourceDirectory = path.substring(0, path.lastIndexOf('/') + 1);
         var sourceFileName = path.substring(path.lastIndexOf('/') + 1, path.length);

         $cordovaFile.copyFile(sourceDirectory, sourceFileName,  cordova.file.dataDirectory).then(function (success) {
             //$scope.profileImage = cordova.file.dataDirectory + sourceFileName;
             alert("Request Successfully Completed");
         }, function (error) {
             console.dir(error);
             alert("Error: " + error); // returns Error: [object Object]
         });

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

I am working with Onsen2, Cordova6.2.0, cordova-plugin-file 4.2.0, android 6

I am still quite new and will appreciate any solutions to my problem or better approach. Thanks :)


Solution

  • I found it! After hours and hours of experimenting and googling, I found another plugin (Cordova saveImageGallery on Github) which is actually an enhancement of the previous one I was using. Here is my code for those in need though you can get more from the plugin's page

    var params = {data: base64ImageString, prefix: 'img_', format: 'JPG', quality: 80, mediaScanner: true};
    window.imageSaver.saveBase64Image(params,
    
       function (filePath) {
          //console.log('File saved on ' + filePath) = file:///storage/emulated/0/Pictures/img_imageFile.png
          var sourceDirectory = filePath.substring(0, filePath.lastIndexOf('/') + 1);
          var sourceFileName = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.length);
    
          $cordovaFile.copyFile(sourceDirectory, sourceFileName,  cordova.file.dataDirectory).then(function (success) {
              alert("Request Successfully Completed");
          }, function (error) {
             console.dir(error);
             alert("Error: " + error);
          });
       },
       function (msg) {
          console.error(msg);
       }
    );
    

    Remember to install the plugin first. Thanks to anyone who tried looking into this :)