无法通过Phonegap访问Android设备中的摄像头

时间:2014-03-18 09:50:40

标签: java android cordova

我正在尝试使用phonegap从网页文件访问我的设备的相机。我完成了以下步骤。

我从以下链接创建了HTML和JavaScript示例文件。 http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#cameraOptions

我下载了最新的phonegap 2.9.1并将cordova文件复制到我的assets / www文件夹中。

我将config.xml文件粘贴到res / xml文件夹中。

我将cordova-2.2.0.jar文件包含在libs文件夹中。

我从我的java类调用HTML文件。

但它显示以下错误。

03-18 15:19:00.364: E/Web Console(15868): Uncaught TypeError: Cannot read property 'DATA_URL' of undefined:92
03-18 15:19:00.864: V/WebViewInputDispatcher(15868): blockWebkitDraw
03-18 15:19:00.864: V/WebViewInputDispatcher(15868): blockWebkitDraw lockedfalse
03-18 15:19:01.169: D/webview(15868): blockWebkitViewMessage= false
03-18 15:19:01.174: E/Web Console(15868): Uncaught TypeError: Cannot read property 'DATA_URL' of undefined:92

请帮我解决我的错误。

3 个答案:

答案 0 :(得分:0)

尝试使用升级版本。像phonegap 3.4.0这样的最新版本可以使用

答案 1 :(得分:0)

首先,我怀疑使用cordova-2.2.0.jar和2.9.0的文档并不好用,而不是用户cordova-2.9.0.jar更好吗? 只是为了确定:config.xml中是否包含相机功能(插件)?

还有一些相关的问题,如何解决类似的问题: cordova giving TypeError: Cannot read property 'DATA_URL' of undefined:68 Can not read propery 'DATA_URL' of undefined type at file:///android_asset/www/apis/camera.js:45

答案 2 :(得分:0)

DATA_URL 更改为 FILE_URI 在getPhoto()函数中

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(imageURI) {
      // 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 = imageURI;
    }

    // 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.FILE_URI , saveToPhotoAlbum: true });
    }

    // 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.FILE_URI,
        sourceType: source });
    }

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