Manik Mahajan
Manik Mahajan

Reputation: 69

Taking screenshot via button in ionic/cordova using screenshot plugin

I want my ionic project to take a screenshot and store it in camera roll. currently the button is not able to take any screenshot. I am testing the app on an android device. I am using this plugin: https://github.com/gitawego/cordova-screenshot

index.html

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet"> 
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="js/ng-cordova.min.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">
    <ion-view>
      <ion-content>
        <button class="button" ng-click="$cordovaScreenshot.capture()">screenshot</button>
      </ion-content>
    </ion-view>
  </body>
</html>

app.js

angular.module('starter', ['ionic','ngCordova'])
.service('$cordovaScreenshot', ['$q', function($q) {
    return {
        capture: function(filename, extension, quality) {
        filename = filename || 'pic';
            extension = extension || 'jpg';
            quality = quality || '100';

            var defer = $q.defer();

            navigator.screenshot.save(function(error, res) {
                if (error) {
                    console.error(error);
                    defer.reject(error);
                } else {
                    console.log('screenshot saved in: ', res.filePath);
                    defer.resolve(res.filePath);
                }
            }, extension, quality, filename);

            return defer.promise;
        }
    };
}]);

Upvotes: 0

Views: 2509

Answers (2)

Manik Mahajan
Manik Mahajan

Reputation: 69

The problem was rectified. The code can be found at

https://github.com/manik1596/coredovaScreenshotShare

Upvotes: 1

Vadiem Janssens
Vadiem Janssens

Reputation: 4109

As a follow-up on my first comment. I think you need a controller between the view (HTML) and your Angular service. A HTML view can't directly communicate with a service, so we need a intermediate controller.

Something along the lines of:

angular.module('starter', ['ionic','ngCordova'])
.controller('myController', ['$cordovaScreenshot', function($cordovaScreenshot) {
    $scope.captureScreenshot = function() {
        $cordovaScreenshot.capture('filename', 'png', 100).then(function(result) {
            // do something with result
        }, function(error) {
            // do something with error
        });
    };
}]);

As you can see, we're using dependency injection to inject the $cordovaScreenshot service.

And your view will trigger the captureScreenshot method:

<ion-content ng-controller="myController">
    <button class="button" ng-click="captureScreenshot()">screenshot</button>
</ion-content>

Notice the ng-controller and a change in the ng-click method.

Upvotes: 2

Related Questions