Phonegap插件 - imagePicker - 生成错误消息

时间:2015-08-01 01:58:28

标签: javascript jquery cordova jquery-mobile phonegap-plugins

我正在尝试将phonegap imagePicker插件添加到我的应用中,并在尝试调用该函数时收到以下错误:

  

“未捕获的TypeError:无法读取未定义的属性'getPictures'”,   来源:file:///android_asset/www/js/main.js(85)

我按照说明使用CLI安装了插件。我已将.js复制到相应的文件夹并引用它。

以下是不同页面的相关部分:

HTML:

<!DOCTYPE html>

<html>
  <head>
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" />

    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <link href="jquerymobile/jquery.mobile-1.4.5.css" rel="stylesheet" type="text/css" />

    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="jquerymobile/jquery.mobile-1.4.5.min.js" type="text/javascript"></script>
    <script src="js/imagepicker.js" type="text/javascript"></script>
    <script src="js/main.js"></script>
  </head>
  <body>
  <!-- a bunch of irrelevant stuff -->
    <a href="#" data-role="button" data-inline="true" data-icon="arrow-u" data-iconpos="bottom" id="btn_upload">Upload</a> 
  </body>
</html>

main.js:

$(document).on("pageshow", "#thepageinquestion", function() {
    $("#btn_upload").click(function() {
        window.imagePicker.getPictures( // <--- this is line 85
            function(results) {
                for (var i = 0; i < results.length; i++) {
                    console.log('Image URI: ' + results[i]);
                }
            }, function (error) {
                console.log('Error: ' + error);
            }
        );
    }); 
});

imagepicker.js:

cordova.define("com.synconset.imagepicker.ImagePicker", function(require, exports, module) { /*global cordova,window,console*/
/**
 * An Image Picker plugin for Cordova
 * 
 * Developed by Wymsee for Sync OnSet
 */

var ImagePicker = function() {

};

/*
*   success - success callback
*   fail - error callback
*   options
*       .maximumImagesCount - max images to be selected, defaults to 15. If this is set to 1, 
*                             upon selection of a single image, the plugin will return it.
*       .width - width to resize image to (if one of height/width is 0, will resize to fit the
*                other while keeping aspect ratio, if both height and width are 0, the full size
*                image will be returned)
*       .height - height to resize image to
*       .quality - quality of resized image, defaults to 100
*/
ImagePicker.prototype.getPictures = function(success, fail, options) {
    if (!options) {
        options = {};
    }

    var params = {
        maximumImagesCount: options.maximumImagesCount ? options.maximumImagesCount : 15,
        width: options.width ? options.width : 0,
        height: options.height ? options.height : 0,
        quality: options.quality ? options.quality : 100
    };

    return cordova.exec(success, fail, "ImagePicker", "getPictures", [params]);
};

window.imagePicker = new ImagePicker();

});

1 个答案:

答案 0 :(得分:1)

打了几个小时后,终于意识到外部javascript文件没有加载。新手错误,我已经在页面标题中放置了对脚本的引用,而不是在data-role =“page”标记内。

在这里找到答案:

JQuery Mobile Change Page doesnt load JS files