如何从 Ionic 图片库下载图片?

How to download an image from Ionic image gallery?

我正在使用 Ion-gallery 指令并使用图像 url 在 ion-gallery 中显示图像。如何下载相同的图片?

如有任何帮助,我们将不胜感激。

我认为为此你想使用 here 中的 ng-cordova File Transfer Plugin

在这个插件中,您必须选择下载文件并将其存储在您的 SD 卡中。

源代码示例 :

            $scope.downloadFile = function() {
               //URL CONTAINTS IMAGE PATH
               var url = "http://your_ip_address/images/my.jpg";
               var filename = url.split("/").pop();
               alert(filename);
               var targetPath = cordova.file.externalRootDirectory + filename;
               var trustHosts = true
               var options = {};
               alert(cordova.file.externalRootDirectory);
               $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
                 .then(function(result) {
                   // Success!
                   alert(JSON.stringify(result));
                 }, function(error) {
                   // Error
                   alert(JSON.stringify(error));
                 }, function (progress) {
                   $timeout(function () {
                     $scope.downloadProgress = (progress.loaded / progress.total) * 100;
                   })
                 });
            }

仍然如果您遇到问题,那么图像不显示在图库中的原因是因为 Android 媒体在下载或创建文件夹后需要更新。我有同样的问题,但我想我找到了解决方案 here

这是一个允许 Cordova 调用媒体扫描器的插件,只需使用:

 window.MediaScannerPlugin(successCallback, failureCallback)

如需更多帮助,请查看 here, here & here

希望对您有所帮助!!