android - 将 Build.PhoneGap.com 与 A​​ndroid 设备一起使用会导致 'cannot call method ' getPicture' of undefined'

标签 android mobile cordova

我使用 Build.PhoneGap 站点中的示例代码从手机捕获图像,并收到“无法调用未定义的方法‘getPicture’”错误。

在index.html和config.xml中使用以下示例代码,我将压缩目录上传到PhoneGap,将应用程序安装在Thunderbolt Android设备上,并在“navigator.camera.getPicture”部分周围的try catch中capturePhoto() 函数的我收到错误。有谁知道这可能是怎么回事?提前致谢。

完整示例的网址:

http://docs.phonegap.com/en/1.0.0/phonegap_camera_camera.md.html

索引代码(无注释):

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

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

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

    document.addEventListener("deviceready",onDeviceReady,false);

    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }

    function onPhotoDataSuccess(imageData) {
      var smallImage = document.getElementById('smallImage');
      smallImage.style.display = 'block';
      smallImage.src = "data:image/jpeg;base64," + imageData;
    }

    function onPhotoURISuccess(imageURI) {
      var largeImage = document.getElementById('largeImage');
      largeImage.style.display = 'block';
      largeImage.src = imageURI;
    }

    function capturePhoto() {
      try
      { //this throws the error
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
      } catch(exc){
        alert(exc.message);
      }
    }

    function capturePhotoEdit() {
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true }); 
    }

    function getPhoto(source) {
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
        destinationType: destinationType.FILE_URI,
        sourceType: source });
    }

    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>

配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
    <widget xmlns = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        id        = "com.phonegap.SampleImg"
        versionCode="10" 
        version   = "1.1.0">

    <!-- versionCode is optional and Android only -->

    <name>PhoneGap SampleImg</name>

    <description>
        An SampleImg for phonegap build docs. 
    </description>

    <author href="https://build.phonegap.com" email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="412c38242c20282d01322e2c24252e2c20282f6f222e2c" rel="noreferrer noopener nofollow">[email protected]</a>">
        My Name
    </author>
    <!-- to enable individual permissions use the following examples -->
<feature name="http://api.phonegap.com/1.0/battery"/>
<feature name="http://api.phonegap.com/1.0/camera"/>
<feature name="http://api.phonegap.com/1.0/contacts"/>
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/geolocation"/>
<feature name="http://api.phonegap.com/1.0/media"/>
<feature name="http://api.phonegap.com/1.0/network"/>
<feature name="http://api.phonegap.com/1.0/notification"/>

</widget>

最佳答案

原来你需要这个引用

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

而不是

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

如果其他人遇到此 build.phonegap 会将适当的phonegap.js 文件与您的目标平台相匹配。

关于android - 将 Build.PhoneGap.com 与 A​​ndroid 设备一起使用会导致 'cannot call method ' getPicture' of undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11760592/

相关文章:

android - 在android textview中捕获http链接点击事件

android - Firebase Analytics VIEW_ITEM_LIST 项目未显示

visual-studio - 使用 azure appcenter 的移动应用程序的诊断日志

cordova - Ionic 无法在 "My"Windows 7 环境上工作

Angular 6 - Cordova - 如何将当前的 Angular 项目转换为基于 cordova 的项目?

Android:下载自动更新后如何打开apk文件?

android - 如何在android中的 Canvas 方法中设置文本的宽度?

c++ - 错误 C2679 : binary '=' : no operator found or there is no acceptable conversion

android - 跨平台移动 C++ SDK 通常如何工作?

javascript - 当我在 eclipse 中清理和构建 android 项目时,它会删除 bin 文件夹和 gen 文件夹并重新生成空的 gen 和 bin 文件夹