为什么我的phonegap相机应用程序无法正常工作?

时间:2014-05-18 18:57:14

标签: android cordova

首先是代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Capture Photo</title>

        <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
        <script type="text/javascript" charset="utf-8">

            var pictureSource,
                destinationType

            document.addEventListener("deviceready",loaded,false);

            function loaded() {
                pictureSource=navigator.camera.PictureSourceType;
                destinationType=navigator.camera.DestinationType;
            }

            function getPhoto(imageData) {
                var smallImage = document.getElementById('smallImage');


                smallImage.style.display = 'block';


                smallImage.src = "data:image/jpeg;base64," + imageData;
            }

            function capturePhoto() {
                navigator.camera.getPicture(getPhoto, onFail, { quality: 50 });
            }


            function onFail(message) {
                alert('Failed because: ' + message);
            }

            </script>
    </head>
    <body>
        <button onclick="capturePhoto();">Capture Photo</button> <br>
        <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    </body>
</html>

我将BuildTool与phonegap(build.phonegap.com)结合使用。我已将此文件压缩为index.zip并将此文件上载到BuildTool中。然后BuildTool为我创建一个应用程序并将此应用程序存储在phonegap上。

当我点击按钮时,没有任何反应。我甚至不会看到失败的警报。

有人知道我在做错了什么吗?我需要包含一些库吗?

注意:我正在尝试将此功能用于Android。

2 个答案:

答案 0 :(得分:1)

pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;

上述两行未完成。它应该像这些

pictureSource=navigator.camera.PictureSourceType.PHOTOLIBRARY;
destinationType=navigator.camera.DestinationType.FILE_URI;

更改后仍然无法正常工作然后尝试这些html工作...

<!DOCTYPE html>
<html>
    <head>
        <title>Submit form</title>
        <script type="text/javascript" src="js/jquery-2.0.2.min.js"></script>
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8">



        function getPhoto() {
            // Retrieve image file location from specified source
            navigator.camera.getPicture(onPhotoURISuccess, onFailure, {
                                        destinationType: navigator.camera.DestinationType.FILE_URI,
                                        sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
                                        });
        }
        function onPhotoURISuccess(imageURI) {
            $("#smallImage").attr('src',imageURI);

        }


        function onFailure()
        {
            alert('failed');
        }



            </script>
    </head>
    <body>
        <br><br><br>

            <button onclick="getPhoto();">Select Photo:</button><br>
            <img style="width:200px;height:200px;" id="smallImage" />

    </body>
</html>

答案 1 :(得分:0)

尝试将deviceready侦听器添加到body加载后调用的函数中,如下所示:

<body onload="onLoad()">

function onLoad() {
    document.addEventListener("deviceready",loaded,false);
}