应用程序在选择图像或拍照后关闭

时间:2012-07-11 19:11:09

标签: javascript android html cordova phonegap-plugins

我在Dreameweaver CS6中使用最新版本的phonegap软件。我从github网站上获取了起始示例HTML文件并将其导入DW。然后,我测试了该应用程序,所有这些都与我的位置,地图等有关。

然而,我现在更改了代码,以便能够拍摄照片或在我的Android手机上选择一张图片,每次我都这样做,或者关闭应用程序。

HTML代码为:

<!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;   // 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 = imageData;
    }

    function onPhotoURISuccess(imageURI) {
      var largeImage = document.getElementById('largeImage');

      largeImage.style.display = 'block';
      largeImage.src = imageURI;
    }

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

    function capturePhotoEdit() {
      // Take picture using device camera, allow edit, and retrieve image as base64-encoded string  
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 100, allowEdit: false }); 
    }

    function getPhoto(source) {
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 100, 
        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="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />
  </body>
</html>

config.xml是:

<?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.xxxxxxxx.DKGApp"
    version         = "1.0.0">

    <name>DKG App</name>

    <description>
        An app that allows uploading their random images they take to our facebook website!
    </description>

    <author href    ="http://www.xxxxxxxx.com"  email       ="mail@xxxxxxxx.com">
        David
    </author>

    <gap:splash src="splash.png" />

    <icon src="icon.png" gap:role="default" />

    <feature name="http://api.phonegap.com/1.0/network" />
    <feature name="http://api.phonegap.com/1.0/camera"/>
    <feature name="http://api.phonegap.com/1.0/file"/>
    <feature name="http://api.phonegap.com/1.0/media"/>

    <preference name="android-minSdkVersion" value="7" />
    <preference name="orientation" value="portrait" />
    <preference name="target-device" value="universal" />
</widget>

我是否遗漏了为了使其工作而不是每次都崩溃所需的东西?

谢谢!

2 个答案:

答案 0 :(得分:0)

您收到内存不足错误。请不要使用DATA_URL。返回3000x2000像素base64编码图像会占用应用中的所有可用内存。改为使用FILE_URI。

答案 1 :(得分:0)

我也面临同样的问题。我使用foreground-camera-plugin解决了这个问题。

转到此链接并按照步骤操作。

https://code.google.com/p/foreground-camera-plugin/