Search code examples
cordovaionic-frameworkcordova-pluginsngcordova

How to download an image from Ionic image gallery?


I am using Ion-gallery directive and displaying images inside ion-gallery using image url. How can I download the same image?

Any help would be appreciated.


Solution

  • I think for this you want to use ng-cordova File Transfer Plugin from here.

    In this plugin you have to option to download file and store in your sd card.

    Source Code Example :

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

    Still If you are having issue then reason for images not to show up in the gallery is because the Android Media needs to be updated after doing downloads or creation of folders. I have the same issue but I think I found a solution here.

    This is a plugin that allows Cordova to invoke Media Scanner just by using:

     window.MediaScannerPlugin(successCallback, failureCallback)
    

    for more help please look to here, here & here.

    Hope this will help you !!