繁体   English   中英

未捕获的ReferenceError:在文件:///android_asset/www/index.html:27上未定义show_pic

[英]Uncaught ReferenceError: show_pic is not defined at file:///android_asset/www/index.html:27

我从Eclipse中收到以下错误(09-13 15:53:10.266:E / Web Console(24208):未捕获的ReferenceError:show_pic未在file:///android_asset/www/index.html:27定义)我尝试调用存储在main.js中的此函数:

function show_pic() {
navigator.camera.getPicture(dump_pic, captureError, {
    quality : 50
});
}

这是HTML

 <!DOCTYPE HTML>
<html>
  <head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
<script src="cordova-2.0.0.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-     1.1.1.min.css" />
  <body>
  <div data-role="page">
  <div data-role="content">
  <div style="text-align:center;margin:20px;">
        <img id="cameraPic" src="" style="width:120px;height:120px;"></img>
    </div>
  </div>
    <div data-role="footer" data-position="fixed">      
        <div data-role="navbar">
            <ul>
                <li><a href="#">One</a></li>
                <li><a href="#" onClick="show_pic()">Two</a></li>
                <li><a href="#">Three</a></li>
            </ul>
        </div><!-- /navbar -->
    </div><!-- /footer -->
  </body>
    </html>

我修改了您的代码。 据我了解,单击navbar按钮Two时,您想显示您拍摄的照片或在指定位置的照片。

根据您要执行的操作(显示正在拍摄的图片或显示设备中的图片等),我定义了3个不同的函数show_pic ,您可以尝试使用它们并使用满足您需要的功能(其中2个已经在评论中)。

另外,考虑到我的评论中的先前建议,我将您的函数show_pic()直接包含在HTML代码内(在script标记中),而不是main.js

因此,这是修改后的HTML文件:

<!DOCTYPE html>
<html>
    <head>

        <meta name="viewport" content="width=device-width" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">

        <!--  BASIC INCLUDES (TO CHANGE ACCORDING TO YOU) -->    

        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile.structure-1.1.0.min.css" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />

        <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>      
        <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>

        <!--  END - BASIC INCLUDES --> 

        <script type="text/javascript" charset="utf-8">

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

            // Wait for Cordova to connect with the device
            //
            document.addEventListener("deviceready",onDeviceReady,false);

            // Cordova is ready to be used!
            //
            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;
            }

// YOUR FUNCTIONS `show_pic()` --------------------------------------------------
            // YOUR FUNCTION `show_pic()`
            //
            function show_pic() {
                // Take picture using device camera and retrieve image as base64-encoded string
                navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
                                        destinationType: destinationType.DATA_URL });
            }

//            // YOUR FUNCTION `show_pic()`. Ex of use: onclick="show_pic(pictureSource.PHOTOLIBRARY);"
//            //
//            function show_pic() {
//                // 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 });
//            }
//            
//            // YOUR FUNCTION `show_pic()`
//            //
//            function show_pic(source) {
//                // Retrieve image file location from specified source
//                navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
//                                            destinationType: destinationType.FILE_URI,
//                                            sourceType: source });
//            }

// END - YOUR FUNCTION `show_pic()` ----------------------------------------------

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


            </script>

    </head>
    <body>
        <div data-role="page">
            <div data-role="content">
                <div style="text-align:center;margin:20px;">
                    <img id="cameraPic" src="" style="width:120px;height:120px;"></img>
                </div>
            </div>
            <div data-role="footer" data-position="fixed">      
                <div data-role="navbar">
                    <ul>
                        <li><a href="#">One</a></li>
                        <li><a href="#" onClick="show_pic()">Two</a></li>
                        <li><a href="#">Three</a></li>
                    </ul>
                </div><!-- /navbar -->
            </div><!-- /footer -->
        </div>
    </body><!-- <-- You forgot this missing DIV -->

</html>

PS:

  • 由于您是从外部网站(http://code.jquery.com/)导入CSS / JS文件,因此请确保已将适当的白名单规则添加到文件res/xml/cordova.xml 我想您可能需要添加类似<access origin="http://code.jquery.com/" /> 检查此链接以获取更多信息: http : //docs.phonegap.com/en/2.0.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide

  • 另外,由于您是在Android上进行开发,因此请确保通过添加以下内容来修改文件app/res/xml/<plugin name="Camera" value="org.apache.cordova.CameraLauncher" />和清单通过添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />来添加app/AndroidManifest


希望这可以帮助。 如果您有任何疑问,可以问我。

另外,有关更多有用的信息,您可能想查看在线文档: http : //docs.phonegap.com/en/2.0.0/cordova_camera_camera.md.html#camera.getPicture

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM