Phonegap相机无法正常工作/入门

时间:2015-03-19 13:32:28

标签: android cordova camera phonegap-plugins

我使用Plugman安装了Phonegap相机插件,并尝试运行cordova Page上提供的示例代码。 我面临的问题是当我运行应用程序时,它不会在我的手机中打开相机。 (点击捕捉照片相机无法打开)。

注意:我已修改

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

在config.xml中

更改了destinationType。在index.html

destinationType: destinationType.DATA_URL,

并且已经提供了所有的权限。

编辑1:

index.html代码:

<!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文件

<?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>

清单:

<?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>

0 个答案:

没有答案