2

I am receiving the following error from eclipse (09-13 15:53:10.266: E/Web Console(24208): Uncaught ReferenceError: show_pic is not defined at file:///android_asset/www/index.html:27) when I attempt to call this function stored in main.js:

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

Here is the 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>
4

1 回答 1

1

我修改了你的代码。如果我理解得很好,您想在单击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" />,并app/AndroidManifest通过添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> .


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

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

于 2012-09-14T01:57:30.790 回答