Cordova相机插件无法在Android手机中运行,但在模拟器中工作

时间:2015-06-07 16:46:56

标签: android apache cordova jquery-mobile android-camera

我开发了一款应用来测试在Android手机中打开的相机。我已经关注了cordova相机API文档。我的代码在模拟器中运行良好且完美,但在移动设备中打开相机时无法打开。

这是我的代码:config.xml

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps" id="io.cordova.CameraTest" version="1.0.0" xmlns="http://www.w3.org/ns/widgets">
  <name>CameraTest</name>
  <description>A blank project that uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
  <author href="http://cordova.io" email="dev@cordova.apache.org">Apache Cordova Team </author>
  <content src="index.html" />
  <access origin="*" />
  <preference name="SplashScreen" value="screen" />
  <preference name="windows-target-version" value="8.0" />
  <preference name="windows-phone-target-version" value="8.1" />
  <vs:plugin name="org.apache.cordova.camera" version="0.3.2" />
  <vs:plugin name="org.apache.cordova.dialogs" version="0.2.10" />
  <feature name="http://api.phonegap.com/1.0/device" />
<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"/>
<feature name="http://api.phonegap.com/1.0/network"/>
  <vs:platformSpecificValues />
</widget>

Js代码:

document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

    function onDeviceReady() {
        // Handle the Cordova pause and resume events
        pictureSource = navigator.camera.PictureSourceType;
        destinationType = navigator.camera.DestinationType;
        document.addEventListener( 'pause', onPause.bind( this ), false );
        document.addEventListener( 'resume', onResume.bind( this ), false );

        // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    };

脚本:

    <script src="cordova.js"></script>
<script>
         var pictureSource;
                var destinationType;
                function opencam() {

                    navigator.camera.getPicture(onsucess, onfail, {
                        quality: 50,
                        destinationType: destinationType.DATA_URL,
                        allowEdit: true,
                        targetWidth: 100,
                        targetHeight: 100,
                        saveToPhotoAlbum: false
                    });
                }

                function onsucess(imageData) {

                    debugger;
                }
                function onfail(message) {
                    alert("Error: " + message);
                    debugger;
                }
</script>

它在纹波模拟器中运行良好,但是当我在移动设备上安装时,相机正在打开。

注意:我的解决方案中没有androidmanifest.xml文件。

1 个答案:

答案 0 :(得分:0)

当您调用opencam()函数时,它不是很清楚,您确定在设备准备好后调用它吗?也许模拟器只是速度较慢,并且在相机代码初始化之后执行相机方法,而在设备上调用来得太快。

也不要在相机插件的回调中使用阻塞方法(调试器,警报)我遇到了问题(我已经看过一些关于它的帖子)。使用控制台o在页面上显示内容。

最后的建议不要使用DATA_URL来检索图像,使用此选项cordova传递带图像的base64字符串,但图像在您的真实设备上可能非常大(并且最差的base64比原始格式大30%)所以它很容易崩溃你的应用程序。尝试使用FILE_URI会更好。

希望这有帮助