2012-08-01 22 views
2

我使用Build.PhoneGap站點的示例代碼從手機中捕獲圖像並獲取「無法調用方法」 getPicture'未定義「錯誤。使用Build.PhoneGap.com android設備導致'can not call method'getPicture'undefined'

在index.html和以下config.xml中使用以下示例代碼,我將壓縮目錄上傳到PhoneGap,在Thunderbolt Android設備上安裝應用程序,並在'navigator.camera.getPicture' capturePhoto()函數的一部分,我得到錯誤。有誰知道這可能會發生什麼?提前致謝。

網址全樣本:

http://docs.phonegap.com/en/1.0.0/phonegap_camera_camera.md.html

指數代碼(沒有評論):

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

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

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

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

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

    function onPhotoDataSuccess(imageData) { 
     var smallImage = document.getElementById('smallImage'); 
     smallImage.style.display = 'block'; 
     smallImage.src = "data:image/jpeg;base64," + imageData; 
    } 

    function onPhotoURISuccess(imageURI) { 
     var largeImage = document.getElementById('largeImage'); 
     largeImage.style.display = 'block'; 
     largeImage.src = imageURI; 
    } 

    function capturePhoto() { 
     try 
     { //this throws the error 
     navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 }); 
     } catch(exc){ 
     alert(exc.message); 
     } 
    } 

    function capturePhotoEdit() { 
     navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true }); 
    } 

    function getPhoto(source) { 
     navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
     destinationType: destinationType.FILE_URI, 
     sourceType: source }); 
    } 

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

    </script> 
    </head> 
    <body> 
    <button onclick="capturePhoto();">Capture Photo</button> <br> 
    <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br> 
    <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br> 
    <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br> 
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" /> 
    <img style="display:none;" id="largeImage" src="" /> 
    </body> 
</html> 

配置文件:

<?xml version="1.0" encoding="UTF-8" ?> 
    <widget xmlns = "http://www.w3.org/ns/widgets" 
     xmlns:gap = "http://phonegap.com/ns/1.0" 
     id  = "com.phonegap.SampleImg" 
     versionCode="10" 
     version = "1.1.0"> 

    <!-- versionCode is optional and Android only --> 

    <name>PhoneGap SampleImg</name> 

    <description> 
     An SampleImg for phonegap build docs. 
    </description> 

    <author href="https://build.phonegap.com" email="[email protected]"> 
     My Name 
    </author> 
    <!-- to enable individual permissions use the following examples --> 
<feature name="http://api.phonegap.com/1.0/battery"/> 
<feature name="http://api.phonegap.com/1.0/camera"/> 
<feature name="http://api.phonegap.com/1.0/contacts"/> 
<feature name="http://api.phonegap.com/1.0/file"/> 
<feature name="http://api.phonegap.com/1.0/geolocation"/> 
<feature name="http://api.phonegap.com/1.0/media"/> 
<feature name="http://api.phonegap.com/1.0/network"/> 
<feature name="http://api.phonegap.com/1.0/notification"/> 

</widget> 

回答

3

原來你需要有這個參考

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

而不是

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

萬一別人是反對這個build.phonegap跑起來會匹配合適phonegap.js文件到平臺廣告定位。

0

http://phonegap.com/download/安裝最新版本的cordova並添加到您的文件中。我希望你的問題得到解決。由於科爾多瓦的舊版本,這種形式可能不支持。

0

@Kevin Shah - phonegap.js不是您需要下載或包含在您的目錄中的文件。基於您的應用程序所針對的平臺(iOS,Adroid等),phonegap會爲您插入適當的「phonegap.js」文件。他們這樣做是因爲每個平臺的行爲不同 - 所以他們需要不同的.js文件。

您需要做的只是包括通用腳本分支,它會自動指向phonegap爲所需平臺插入的正確文件。