简体   繁体   中英

PhoneGap Camera , Cannot read property 'getPicture' of undefined

I am trying to get the Camera API to work in my PhoneGap android app, but i keep getting this error

"Cannot read property 'getPicture' of undefined".

Now i have checked countless answers on StackOverflow and tutorials all over the web,and tried all the answer there(with no luck), and i cant seem to find the issue.

This is the button that calls the function

<button type="button" class="btn btn-primary" ng-click="getPic()">Camera</button>

This is the controller that handles the camera

myApp.controller('EditProfileCtrl', function ($scope, $http, navigateFactory) {
$scope.getPic = function () {
    navigator.camera.getPicture(onSuccess, onFail, {
        quality: 60,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: 1
    });
    function onSuccess(imageData) {
        var image = document.getElementById('myImage');
        image.src = "data:image/jpeg;base64," + imageData;
    }
    function onFail(message) {
        alert('Failed beause' + message);
    }
};
});

Please comment if there is any additional information required. Any and all help will be hugely appreciated.

EDIT: So after following Aravin's advice i added <script src="cordova.js"></script> now it atleast looks like something is happening, but now im getting these errors in my eclipse logcat:

I/System.out(3871): Error adding plugin org.apache.cordova.CameraLauncher D/PluginManager(3871): exec() call to unknown plugin: Camera

The total work around is below..

you need to write all code in your html page.

<body>
    <div data-role="page"> 
        <script type="text/javascript"> 
        function getPhotoFromCamera() { 
                navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
                quality: 50, 
                sourceType: navigator.camera.PictureSourceType.CAMERA, 
                destinationType: navigator.camera.DestinationType.DATA_URL, 
            }); 
        } 
        function onPhotoDataSuccess(imageData){ 
            var image = document.getElementById('image'); 
            image.style.display = 'block'; 
            image.src = "data:image/jpeg;base64,"+imageData; 
        } 
        function getPhotoFromAlbum(){ 
            navigator.camera.getPicture(onPhotoURISuccess, onFail,{ 
                quality: 50, 
                sourceType: navigator.camera.PictureSourceType.SAVEDPHOTOALBUM, 
                destinationType: navigator.camera.DestinationType.FILE_URI 
            }); 
        } 
        function onPhotoURISuccess(imageURI){ 
            var image = document.getElementById('image'); 
            image.style.display = 'block'; 
            image.src = imageURI; 
        } 
        function onFail(message){ 
            alert('Failed because:' + message); 
        } 
        </script> 

        <div data-role="header" style="height:36px;"> 
            <h1>Write New</h1> 
            <a href="#" data-icon="arrow-l" data-rel="back">Back</a> 
        </div> 

        <button onclick="getPhotoFromCamera();">Launch Camera</button> 
        <button onclick="getPhotoFromAlbum();">Goto Picture Gallery</button> 
        <div> 
            <img id="image" style="display:none;width;290px;height:210px;" src = ""/> 
        </div> 

    </div> 
<script type="text/javascript" src="cordova.js"></script>

</body>

So after trying all the things here with no success I did this which fixed the issue (dont know how)...
remove the plugin , build , add the plugin , build
and Magic!

如果您正在使用TFS,请记得在安装插件之前检查编辑“fetch.json”,否则它将在安装中失败。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM