Cordova Network and Camera API returns undefined

别等时光非礼了梦想. 提交于 2019-12-12 04:54:09

问题


Building my first Cordova 4.0 app, and I would really need some help since I'm gonna show a demo of this app tmrw...

When I try to access the Network info API and the Camera API (navigator.connection and navigator.camera respectively) they always return undefined.

I have these right in my Android manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And here are the installed plugins:

org.apache.cordova.camera 0.3.3 "Camera"
org.apache.cordova.console 0.2.11 "Console"
org.apache.cordova.device 0.2.12 "Device"
org.apache.cordova.network-information 0.2.13 "Network Information"

I have also copied the cordova.js file from the platform-folder and added the <SCRIPT TYPE="text/javascript" src="js/cordova.js"></SCRIPT> tag to my index.html. Also here's an example of my code where I try to access the camera API:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    $(document).on('click', '.camera', function(){

        if (!navigator.camera) {
            alert("Camera API not supported", "Error");
            return;
        }
        var options =   {   quality: 50,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: 1,      // 0:Photo Library, 1=Camera, 2=Saved Album
            encodingType: 0     // 0=JPG 1=PNG
        };
        navigator.camera.getPicture( function(imgData) { return imgData; }, function() { alert('Error');}, options);

    });
}

When I run this I always get the "Camera API not supported" alert, meaning it's undefined.

What am I missing?


回答1:


So the answer to this question was that Cordova automatically includes the cordova.js file in the projects www on build. So all I had to do was include the <script type="text/javascript" src="cordova.js"></script>tag.

That is, my copying the cordova.js file and including it into my JS-folder was unnecessary. Hope this might help somebody.



来源:https://stackoverflow.com/questions/26960901/cordova-network-and-camera-api-returns-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!