1

I installed Phonegap camera plugin using Plugman and tried running sample code provided on cordova Page. Problem I am facing is when I run app, it does not open camera in my phone. (on clicking capture photo Camera fails to open).

Note: I have modified

    <feature name="Camera">
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
    </feature> 

in config.xml

Changed destinationType. in index.html

destinationType: destinationType.DATA_URL,

and have given all permissions as mentioned.

Edit 1 :

index.html code:

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready",onDeviceReady,false);

    // device APIs are available
    //
    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }

    // Called when a photo is successfully retrieved
    //
    function onPhotoDataSuccess(imageData) {
      // Uncomment to view the base64-encoded image data
      // console.log(imageData);

      // Get image handle
      //
      var smallImage = document.getElementById('smallImage');

      // Unhide image elements
      //
      smallImage.style.display = 'block';

      // Show the captured photo
      // The inline CSS rules are used to resize the image
      //
      smallImage.src = "data:image/jpeg;base64," + imageData;
    }

    // Called when a photo is successfully retrieved
    //
    function onPhotoURISuccess(imageURI) {
      // Uncomment to view the image file URI
      // console.log(imageURI);

      // Get image handle
      //
      var largeImage = document.getElementById('largeImage');

      // Unhide image elements
      //
      largeImage.style.display = 'block';

      // Show the captured photo
      // The inline CSS rules are used to resize the image
      //
      largeImage.src = imageURI;
    }

    // A button will call this function
    //
    function capturePhoto() {
      // Take picture using device camera and retrieve image as base64-encoded string
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
        destinationType: destinationType.DATA_URL });
    }

    // A button will call this function
    //
    function capturePhotoEdit() {
      // Take picture using device camera, allow edit, and retrieve image as base64-encoded string
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
        destinationType: destinationType.DATA_URL });
    }

    // A button will call this function
    //
    function getPhoto(source) {
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
        destinationType: destinationType.DATA_URL,
        sourceType: source });
    }

    // Called if something bad happens.
    //
    function onFail(message) {
      alert('Failed because: ' + message);
    }

    </script>
  </head>
  <body>
    <button onclick="capturePhoto();">Capture Photo</button> <br>
    <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />
  </body>
</html>

Config.xml file

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.helloCordova" version="2.0.0" xmlns="http://www.w3.org/ns/widgets">
    <name>Hello Cordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <access origin="*" />
    <content src="index.html" />
    <preference name="loglevel" value="DEBUG" />
    <feature name="App">
        <param name="android-package" value="org.apache.cordova.App" />
    </feature>
    <feature name="Geolocation">
        <param name="android-package" value="org.apache.cordova.GeoBroker" />
    </feature>
    <feature name="Device">
        <param name="android-package" value="org.apache.cordova.Device" />
    </feature>
    <feature name="Accelerometer">
        <param name="android-package" value="org.apache.cordova.AccelListener" />
    </feature>
    <feature name="Compass">
        <param name="android-package" value="org.apache.cordova.CompassListener" />
    </feature>
    <feature name="Media">
        <param name="android-package" value="org.apache.cordova.AudioHandler" />
    </feature>
    <feature name="Camera">
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
    </feature>
    <feature name="Contacts">
        <param name="android-package" value="org.apache.cordova.ContactManager" />
    </feature>
    <feature name="File">
        <param name="android-package" value="org.apache.cordova.FileUtils" />
    </feature>
    <feature name="NetworkStatus">
        <param name="android-package" value="org.apache.cordova.NetworkManager" />
    </feature>
    <feature name="Notification">
        <param name="android-package" value="org.apache.cordova.Notification" />
    </feature>
    <feature name="Storage">
        <param name="android-package" value="org.apache.cordova.Storage" />
    </feature>
    <feature name="FileTransfer">
        <param name="android-package" value="org.apache.cordova.FileTransfer" />
    </feature>
    <feature name="Capture">
        <param name="android-package" value="org.apache.cordova.Capture" />
    </feature>
    <feature name="Battery">
        <param name="android-package" value="org.apache.cordova.BatteryListener" />
    </feature>
    <feature name="SplashScreen">
        <param name="android-package" value="org.apache.cordova.SplashScreen" />
    </feature>
    <feature name="Echo">
        <param name="android-package" value="org.apache.cordova.Echo" />
    </feature>
    <feature name="Globalization">
        <param name="android-package" value="org.apache.cordova.Globalization" />
    </feature>
    <feature name="InAppBrowser">
        <param name="android-package" value="org.apache.cordova.InAppBrowser" />
    </feature>
    <plugins>
        <plugin name="Capture" value="org.apache.cordova.Capture"/>
    </plugins>
</widget>

Manifest:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0" android:windowSoftInputMode="adjustPan" package="com.socialnoise.testapp" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="testapp" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="22" />
</manifest>
Nehil Mistry
  • 1,101
  • 2
  • 22
  • 51
  • show me your full code here. – Nurdin Mar 19 '15 at 14:50
  • Code Added @Dato'MohammadNurdin – Nehil Mistry Mar 20 '15 at 04:42
  • Do you have any error logs in the console? You can try to see if running this command from a shell in your computer opens the camera app : `adb shell am start -a android.media.action.IMAGE_CAPTURE` (it's what the camera app internally launches) – QuickFix Mar 20 '15 at 08:19
  • When I fire query in adb shell, My camera gets started but when i click button created by above code it does not open camera. Error I got(logcat result) 03-20 17:02:04.253: D/CordovaLog(11677): file:///android_asset/www/index.html: Line 67 : Uncaught TypeError: Cannot read property 'getPicture' of undefined 03-20 17:02:04.253: I/chromium(11677): [INFO:CONSOLE(67)] "Uncaught TypeError: Cannot read property 'getPicture' of undefined", source: file:///android_asset/www/index.html (67) @QuickFix – Nehil Mistry Mar 20 '15 at 11:32
  • seems the plugin is corrupted or not installed correctly. have you tried to remove and reinstall the plugin using `cordova plugin remove org.apache.cordova.camera` and then `cordova plugin add org.apache.cordova.camera`? Sometimes you may also need to remove/add the android platform when some json file listing the plugins gets corrupted. – QuickFix Mar 20 '15 at 16:40
  • and you should not manually change feature in config.xml or add any permissions, it's done when you run cordova plugin add. – QuickFix Mar 20 '15 at 16:42
  • Error I am getting - Cannot read property 'trim' of null – Nehil Mistry Mar 23 '15 at 13:27
  • Maybe this answer helps: http://stackoverflow.com/a/35315002/279712 – Paulo Coghi Feb 10 '16 at 12:16

0 Answers0