TheArtofXin
TheArtofXin

Reputation: 33

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

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>

Upvotes: 2

Views: 4295

Answers (1)

Littm
Littm

Reputation: 4947

I mofidied your code a bit. If I understood well, you want to display a picture that you take or that is in a specified location, when clicking on your navbar button Two.

According to what you want to do (displaying a picture that you are taking, or displaying a picture that is located in your device, etc), I defined 3 different functions show_pic, you may try them and use the one that fits your needs (2 of them are already in comments).

Also, while taking into account the previous suggestion in my comment, I included your function show_pic() directly inside the HTML code (in script tags) instead of main.js:

So here's the modified HTML file:

<!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:

  • Since you're importing CSS / JS files from external web (http://code.jquery.com/), make sure that you've added the appropriate whitelist rule to your file res/xml/cordova.xml. I guess you may need to add something like <access origin="http://code.jquery.com/" /> . Check this link for more information: http://docs.phonegap.com/en/2.0.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide .

  • Also, since you're developing on Android, make sure that you modify your file app/res/xml/ by adding: <plugin name="Camera" value="org.apache.cordova.CameraLauncher" />, and your manifest app/AndroidManifest by adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> .


Hope this helps. If you have any questions, you can ask me.

Also, for more useful information, you may wanna check the online doc: http://docs.phonegap.com/en/2.0.0/cordova_camera_camera.md.html#camera.getPicture

Upvotes: 1

Related Questions