【问题标题】:Copy image file from /storage/emulated/0/Pictures to another directory in cordova将图像文件从 /storage/emulated/0/Pictures 复制到科尔多瓦的另一个目录
【发布时间】:2016-06-09 15:54:35
【问题描述】:

我正在构建一个小型混合应用程序。我使用cordova-base64-to-gallery plugin 将base64 图像从裁剪图像(使用Croppie)转换为png 文件,然后将其存储在手机图片库中的图片文件夹中。到目前为止,将 base64 字符串保存到图像效果很好,我可以在图像库中看到它。但我的问题是如何将返回 path 作为 /storage/emulated/0/Pictures/img_imageFile.png 的图像复制到 cordova.file.dataDirectory 使用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

我还是个新手,我会感谢我的问题的任何解决方案或更好的方法。谢谢:)

【问题讨论】:

    标签: angularjs cordova ngcordova


    【解决方案1】:

    我找到了!经过数小时的试验和谷歌搜索,我发现了另一个插件 (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);
       }
    );
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2017-08-21
      相关资源
      最近更新 更多