Search code examples
androidcordovacordova-pluginscordova-2.0.0

Cordova camera not working


We've tried to open the camera from android device using phonegap but not able to do that. Can you please help us with that.

Below is the code written.

<div>
        <input class="camerabutton" id="btnCamera" value="open camera" type="button" height="100" width="100" />
       </div>
           <script type="text/javascript" src="js/cordova.js"></script>
        <script type="text/javascript" src="js/cordova_plugins.js"></script>
        <script type="text/javascript" src="js/framework7.js"></script> 

        <script type="text/javascript">
        var myApp = new Framework7();

// Export selectors engine
var $$ = Dom7;

$$('.camerabutton').on('click', function () {

      navigator.camera.getPicture(onSuccess, onFail, {
                quality: 50
            });


        function onSuccess(imageData) {
          alert("test");
        }

        function onFail(message) {
            alert('Failed because: ' + message);
        }
});
  </script>

Config.xml file(Given all the permissions)

  <feature name="http://api.phonegap.com/1.0/device" />
<feature name="http://api.phonegap.com/1.0/camera"/>
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/media"/>
<feature name="http://api.phonegap.com/1.0/network"/>

AndroidManifest.xml file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

Let us know if any issue in mentioned code above or need to add something.


Solution

  • Huh, after doing some research we can able to fix that issue. It was the issue in config.xml so I replaced that with the code mentioned below.

    <?xml version='1.0' encoding='utf-8'?>
    <widget id="com.example.test" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
        <name>test</name>
        <description>
            A sample Apache Cordova application that responds to the deviceready event.
        </description>
        <author email="[email protected]" href="http://cordova.io">
            Apache Cordova Team
        </author>
        <content src="Contacts.html" />
        <plugin name="cordova-plugin-whitelist" spec="1" />
        <access origin="*" />
        <allow-intent href="http://*/*" />
        <allow-intent href="https://*/*" />
        <allow-intent href="tel:*" />
        <allow-intent href="sms:*" />
        <allow-intent href="mailto:*" />
        <allow-intent href="geo:*" />
        <platform name="android">
            <allow-intent href="market:*" />
        </platform>
        <platform name="ios">
            <allow-intent href="itms:*" />
            <allow-intent href="itms-apps:*" />
        </platform>
        <feature name="Contacts">
       <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
    </feature>
    <feature name="Camera">
    <param name="android-package" value="org.apache.cordova.CameraLauncher" />
    </feature>
    </widget>