Cordova camera plugin not working in Android mobile but working in emulator

2.3k views Asked by At

I have developed an app to test the camera open in Android mobiles. I have followed the cordova camera API document. My code works fine and perfect in emulator, but not opening when I opens the camera in mobile.

Here is my code: config.xml

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="io.cordova.CameraTest" version="1.0.0" xmlns="http://www.w3.org/ns/widgets">
  <name>CameraTest</name>
  <description>A blank project that uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
  <author href="http://cordova.io" email="[email protected]">Apache Cordova Team </author>
  <content src="index.html" />
  <access origin="*" />
  <preference name="SplashScreen" value="screen" />
  <preference name="windows-target-version" value="8.0" />
  <preference name="windows-phone-target-version" value="8.1" />
  <vs:plugin name="org.apache.cordova.camera" version="0.3.2" />
  <vs:plugin name="org.apache.cordova.dialogs" version="0.2.10" />
  <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"/>
  <vs:platformSpecificValues />
</widget>

Js code:

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

    function onDeviceReady() {
        // Handle the Cordova pause and resume events
        pictureSource = navigator.camera.PictureSourceType;
        destinationType = navigator.camera.DestinationType;
        document.addEventListener( 'pause', onPause.bind( this ), false );
        document.addEventListener( 'resume', onResume.bind( this ), false );

        // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    };

script:

    <script src="cordova.js"></script>
<script>
         var pictureSource;
                var destinationType;
                function opencam() {

                    navigator.camera.getPicture(onsucess, onfail, {
                        quality: 50,
                        destinationType: destinationType.DATA_URL,
                        allowEdit: true,
                        targetWidth: 100,
                        targetHeight: 100,
                        saveToPhotoAlbum: false
                    });
                }

                function onsucess(imageData) {

                    debugger;
                }
                function onfail(message) {
                    alert("Error: " + message);
                    debugger;
                }
</script>

Its working fine in ripple emulator, but when I install in mobile, camera is opening at all.

Note: I don't have androidmanifest.xml file in my solution.

1

There are 1 answers

7
wezzy On

it's not very clear when you call the opencam() function, are you sure that you are calling it after the device ready ? Maybe the emulator is just slower and it executes the camera method after that the camera code is initialized while on device the call comes too soon.

Also do not use blocking method (debugger, alert) in callback from camera plugin I had problems with them (and I've seen some posts about it). Use console o display something on your page.

Last suggestion do not use DATA_URL to retrieve the image, using this option cordova pass a base64 string with the image but the image could be very big on your real device (and worst base64 is 30% bigger than the original format) so it can easily crash your app. Try using FILE_URI it's way better.

Hope this helps