【发布时间】:2016-09-10 15:53:01
【问题描述】:
我的 Android 应用需要拍照,但相机插件不起作用。当我单击按钮时,没有任何反应。
index.html
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"/>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady() {
alert("ready");
}
function capturePhoto()
{
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.FILE_URI });
}
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto()">Capture</button> <br>
<button onclick="onDeviceReady()">alert</button> <br>
<img id="myImage" 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= "testaplikacji"
versionCode= "1"
version = "1.0.0" >
<name>Camera</name>
<description>Camera test</description>
<author>Sebastian</author>
<gap:platform name="android" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="cordova-plugin-camera" />
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<access origin="*"/>
<gap:config-file platform="android" parent="/manifest" mode="add" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</gap:config-file>
</widget>
我尝试了有和没有权限。我尝试了不同的插件。我尝试了这个要点:https://gist.github.com/dhavaln/2238017
还有本教程中的代码: https://www.youtube.com/watch?v=KlBfmHDZjmg
没有任何作用。我浪费了很多时间试图使这个工作。请帮我。
【问题讨论】:
-
您在启动应用程序时是否看到“就绪”消息。 (不点击警报按钮)?
-
是的,每次都有效。
-
尝试使用
adb logcat查看运行应用时是否有任何有意义的提示或警告输出。 -
当我点击“捕获”按钮时,我得到了这个: I/chromium: [INFO:CONSOLE(15)] "Uncaught TypeError: Cannot read property 'getPicture' of undefined",来源:文件: ///data/data/.../files/downloads/app_dir/index.html (15)
-
当您收到
deviceready通知后navigator.camera仍然未定义,这意味着相机插件不存在或无法正常工作。
标签: javascript android html phonegap-plugins phonegap-build