Search code examples
javascriptandroidcordovaionic-framework

Ionic/Cordova - Get Image path to use on <img src="">


I have an Ionic app in which I'll take a screenshot of the app.

My problem is that I need to get the image path to use in an image tag src. But I don't know why isn't happening.

Here's my code:

$scope.image = {
    src: ''
};

$scope.takeScreenshot = function() {

    var screenshotLink,
        screenshot = new Image(),
        filename = '';

    navigator.screenshot.save(function(error,res) {
        if(error){
            //console.error(error);
        } else {  
            screenshotLink = res.filePath;
        }
    },'jpg',100,filename);
           
    $scope.image.src = screenshotLink;

};//end_takeScreenshot


<img ng-src="{{ image.src }}" alt="foobar"/>

Can you guys help me solve this?


Solution

  • You need to move $scope.image.src = screenshotLink into the callback function for navigator.screenshot.save. Like so:

    navigator.screenshot.save(function(error,res) {
        if(error){
            //console.error(error);
        } else {  
            screenshotLink = res.filePath;
            $scope.$apply( function() {
                $scope.image.src = screenshotLink;
            });
        }
    },'jpg',100,filename);