将图像文件从 /storage/emulated/0/Pictures 复制到 cordova 中的另一个目录

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

我正在构建一个小型混合应用程序。我用了 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.

这是我的 angularjs 代码

// 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);                             
     });
 });

我正在使用 Onsen2、Cordova6.2.0、cordova-plugin-file 4.2.0,android 6

我还是个新手,如果能解决我的问题或更好的方法,我将不胜感激。谢谢:)

我找到了!经过数小时的试验和谷歌搜索,我发现了另一个插件 (Cordova saveImageGallery on Github),它实际上是我使用的前一个插件的增强版。 这是我为有需要的人编写的代码,但您可以从插件页面获得更多信息

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);
   }
);

记得先安装插件。感谢任何尝试调查此问题的人:)