4

I am new to angularjs and my project runs on android and IOS platform using angularjs and ionic. I need to save an image into phone photo album(support IOS and Andorid) with the image url. I know cordova plugins can download images from url but I can't understand how to install cordova-plugin-file-transfer. Is there any other easier way to achieve this function? Thanks.

angular.module('myApp', []);

angular.module('myApp').controller('HomeCtrl', ['$scope', '$http', function($scope, $http) {

  $scope.saveImg = function(imgUrl){
   var hideSheet = $ionicActionSheet.show({
                        buttons: [
                            {text: 'save image'},
                            {text: 'copy link'},
                        ],
                        cancelText: 'cancel',
                        cancel: function () {
                        },
                        buttonClicked: function (index) {
                            if (index == 0) {//save image here
                               
                            }
                        }
                    });

  }
 
}]);
<img ng-src="{{imgUrl}}" on-hold="saveImg(imgUrl)">
Talha Awan
  • 4,573
  • 4
  • 25
  • 40
jingyo
  • 41
  • 3
  • You must need to use cordova-plugin-file-transfer – Paresh Gami May 25 '17 at 06:26
  • @jingyo Working sample using vanilla cordova framework - https://github.com/gandhirajan/Cordova_File_Operations – Gandhi May 25 '17 at 07:41
  • thanks for your help. But my App is working with angular. I just can't understand how to install plugin cordova-plugin-file-transfer to cordova. – jingyo May 25 '17 at 08:38

1 Answers1

1

First you need to install the plugin using CLI:

cordova plugin add cordova-plugin-file-transfer

Now inject the dependency on ngCordova in the module and

$cordovaFileTransfer into your controller and add the following code:

var app= angular.module('myApp', ['ngCordova']);

    app.controller('HomeCtrl', ['$scope', '$http', '$cordovaFileTransfer',function($scope, $http,$cordovaFileTransfer) {

    var url = "http://cdn.wall-pix.net/albums/art-space/00030109.jpg"; // your url
        var targetPath = cordova.file.documentsDirectory + "testImage.png"; // filename
        var trustHosts = true;
        var options = {};
    $scope.trans=function(){
        $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
          .then(function(result) {
            // Success!
          }, function(err) {
            // Error
          }, function (progress) {

          });

       }

    }]);

Here is the reference : http://ngcordova.com/docs/plugins/fileTransfer/

PRANSHU MIDHA
  • 472
  • 7
  • 13