Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Cordova camera is not working

I am trying to use ionic cordova camera.

I have below code

HomePage.html

<ion-view view-title="Example">
  <ion-content>
    <img ng-show="imgURI !== undefined" ng-src="{{imgURI}}">
    <img ng-show="imgURI === undefined" ng-src="http://placehold.it/300x300">
    <button class="button" ng-click="takePicture()">Take Picture</button>
  </ion-content>
</ion-view>

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <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="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
  </head>
  <body ng-app="starter">

 <ion-nav-view></ion-nav-view>

</body>
</html>

controllers.js

angular.module('starter.controllers', [])

.controller('HomePageCtrl', function ($scope, $state, $location, $cordovaCamera) {

    $scope.data = {};


    $scope.takePicture = function () {
        var options = {
            quality: 75,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function (imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
        }, function (err) {
            // An error occured. Show a message to the user
        });
    }
})

app.js

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova'])

.run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {

        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);

        }
        if (window.StatusBar) {
            // org.apache.cordova.statusbar required
            StatusBar.styleLightContent();
        }
    });
})

.config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider

         .state('HomePage', {
             url: '/HomePage',
             templateUrl: 'templates/HomePage.html',
             controller: 'HomePageCtrl'
         })
         .state('login', {
             url: '/login',
             templateUrl: 'templates/Login.html',
             controller: 'LoginCtrl'
         });

    $urlRouterProvider.otherwise('/login');  
});

Question:

When i click to button in order to use camera

I get below exception:

 Module 'ngCordova' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Where i miss in above side

Any help will be appreciated.

Thanks.

like image 231
Soner Avatar asked Dec 30 '25 20:12

Soner


1 Answers

Taken frome here:

Include ng-cordova.js or ng-cordova.min.js in your index.html file before cordova.js and after your AngularJS / Ionic file (since ngCordova depends on AngularJS).

In short words: ngCordova is missing in your index.html.

like image 75
Andre Kreienbring Avatar answered Jan 02 '26 08:01

Andre Kreienbring