PhoneGap 捕获插件导致 Android 崩溃

2024-01-02

当我使用捕获插件时,它总是在拍照后使应用程序崩溃。当我单击“捕捉”按钮时,它会打开相机应用程序,我拍照,然后单击复选标记,然后应用程序关闭并显示:“不幸的是,HelloWorld 已停止。”然后,如果我查看图库应用程序,照片就在那里。我做错了什么吗?或者是插件有问题?

这就是我所做的:

我创建了一个全新的phonegap 3.x(确切地说是3.1.0-0.15.0)项目

phonegap create exampleProject
cd exampleProject

并安装了捕获插件

phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-media-capture.git

将 org.apache.cordova.media-capture 放入插件文件夹中。然后我构建了该项目:

phonegap build android

这会将 org.apache.cordova.file 放入插件文件夹中(因为 media-capture 对其有依赖),并在插件文件夹中创建 android.json 。它还创建platforms/android并将正确的js文件放入platforms/android/assets/www/plugins并将正确的java文件放入platforms/android/src

我将以下内容放入index.html:

<input type="button" value="Capture" onClick="capture();" />

这是整个文件:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>PhoneGap</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
            <input type="button" value="Capture" onClick="capture();" />
        </div>
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

并将 capture() 和 getErrorMessage() 函数添加到 index.js 的末尾:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

function capture() {
    var options = { limit: 1 };
    try {
        navigator.device.capture.captureImage(
            function(response) {
                alert("success: response=" + stringify(response));
            },
            function(CaptureError) {
                alert('Error: ' + getErrorMessage(CaptureError));
            },
            options
        );
    } catch(e) {
        alert("catch e="+e);
    }
}

function getErrorMessage(CaptureError) {
  var errorMessage = 'An unknown error occured while trying to get your media, please try again.';
  switch(CaptureError.code) {
    case CaptureError.CAPTURE_NOT_SUPPORTED:
      errorMessage = 'This app does not support the media type.';
      break;
    case CaptureError.CAPTURE_NO_MEDIA_FILES:
      errorMessage = 'No media files returned.';
      break;
    case CaptureError.CAPTURE_INTERNAL_ERR:
      errorMessage = 'The capture process experienced an internal error.';
      break;
    case CaptureError.CAPTURE_APPLICATION_BUSY:
      errorMessage = 'The application was too busy with something else to handle the media capture.';
      break;
    case CaptureError.CAPTURE_INVALID_ARGUMENT:
      errorMessage = 'Values submitted for capture were out of range, notify support.';
      break;
    case 3:
      errorMessage = 'Did you cancel? Please try again.';
      break;
    default:
      break;
  }
  return errorMessage;
}

然后我构建应用程序:

phonegap build android

并在我的 Android 设备上运行它并遇到上述问题


使用 logcat(与 Android SDK 捆绑在一起)查看是否有任何错误消息来自phonegap。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PhoneGap 捕获插件导致 Android 崩溃 的相关文章