【问题标题】:DevExtreme/Phonegap camera plugin: "Cannot read propetry 'getPicture' of undefined"DevExtreme/Phonegap 相机插件:“无法读取未定义的属性 'getPicture'”
【发布时间】:2017-09-12 14:26:41
【问题描述】:

我的应用快疯了。

我正在使用基于 phonegap 的 DevExtreme。

我想在我的应用中使用相机拍照,但遇到了一些问题。

首先,这里是我的 config.xml。

<widget id="com.devexpress.apptemplate" version="1.0" versionCode="1">
  <name>ApplicationTemplate</name>
  <description>Template</description>
  <preference name="permissions" value="none" />
  <preference name="prerendered-icon" value="true" />
  <preference name="android-windowSoftInputMode" value="adjustPan" />
  <preference name="SplashScreenDelay" value="60000" />
  <preference name="AutoHideSplashScreen" value="false" />
  <preference name="SplashShowOnlyFirstTime" value="false" />
  <preference name="FadeSplashScreen" value="false" />
  <preference name="ShowSplashScreenSpinner" value="false" />
  <preference name="DisallowOverscroll" value="true" />
  <preference name="StatusBarOverlaysWebView" value="false" />
  <preference name="StatusBarBackgroundColor" value="#000000" />
  <preference name="android-minSdkVersion" value="14" />
  <preference name="android-targetSdkVersion" value="22" />
  <plugin name="cordova-plugin-camera" spec="^2.4.1" source="npm" />
  <plugin name="cordova-plugin-splashscreen" onload="true" />
  <plugin name="cordova-plugin-whitelist" />
  <plugin name="cordova-plugin-ios-longpress-fix" />
  <plugin name="cordova-plugin-statusbar" onload="true" />
  <access origin="*" />
</widget>

你觉得这里有什么问题吗?

此外,我还有一个按钮可以调用相机功能(从教程中复制):

// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
    try
    {
        // 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;
    }
    catch (err) {
        alert(err.message);
    }
}

// Called when a photo is successfully retrieved
//
function onPhotoFileSuccess(imageData) {
    // Get image handle
    console.log(JSON.stringify(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 = 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 capturePhotoWithData() {
    try {
        // Take picture using device camera and retrieve image as base64-encoded string
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
    }
    catch (err) {
        alert(err.message);
    }
}
function capturePhotoWithFile() {
    navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}

// A button will call this function
//
function getPhoto(source) {
    debugger;
    // Retrieve image file location from specified source
    navigator.camera.getPicture(onPhotoURISuccess, onFail, {
        quality: 50,
        destinationType: OverviewAPP.destinationType.FILE_URI,
        sourceType: source
    });
}
// Called if something bad happens.
// 
function onFail(message) {
    alert('Failed because: ' + message);
}

这里是我的相关 HTML:

[...]
<div class="myBtnPhoto" data-bind="dxButton: { text: 'Conferma con foto', onClick: capturePhotoWithData}" style="float:right;"></div>
[...]
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
[...]

函数 capturePhotoWithData 被正确调用,但函数进入 catch 分支,并且警报显示“无法读取未定义的属性 'getPicture'”

我创建模板并构建解决方案。我正在我的 Android 设备上尝试这个。我在网上阅读了很多论坛和其他问题,但对我来说仍然没有任何效果。

您是否发现这里有任何错误或遗漏?

【问题讨论】:

    标签: javascript cordova camera phonegap devextreme


    【解决方案1】:

    这意味着navigator.camera 未定义,因此您的项目看不到cordova-plugin-camera

    最常见的问题可能是:您在设备准备好之前调用它。 一定要把摄像头叫到deviceready

    document.addEventListener("deviceready", function(){
      console.log(navigator); // here you can check all plugins
    }
    

    您可以运行:$ cordova plugin list 以确保该插件也存在于列表中。

    也尝试重建:$ cordova build android(或ios)

    【讨论】:

    • 好吧,我已经害怕了,但为什么它是未定义的?我疯了,我在其他应用程序中管理其他插件(例如:推送通知),但在这种情况下我被卡住了。我无法运行cordova(或者我不知道如何在 DevExtreme 项目中运行)。无论如何,奇怪的是我在apk插件文件夹中看到它但是当我安装它时,该应用程序只需要数据存储权限而不需要相机权限..
    • 如何在设备准备好之前调用它?应用程序启动后,我必须登录并进入正确的部分,所以,我认为这不可能......还是不??
    • @PieroAlberto 我只是给你工具来检测哪里出了问题。
    • 如何在真机上使用你写的函数?也许有警报?
    • cordova.js 导入丢失
    猜你喜欢
    • 2014-08-29
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多