繁体   English   中英

cordova navigator.camera.getPicture 在 android 中不起作用

[英]cordova navigator.camera.getPicture not working in android

navigator.camera.getPicure 功能不起作用。 它的回调函数永远不会在下面的代码中被触发。 此功能来自cordova相机插件。

navigator.camera.getPicture(
                            uploadPhoto,
                            function(message) {
                                alert('Failed to get a picture. Please select one.');
                            }, {
                                quality         : 50,
                                destinationType : Camera.DestinationType.FILE_URI,
                                sourceType      : Camera.PictureSourceType.SAVEDPHOTOALBUM
                            });

在上面的代码中,uploadPhoto 回调函数永远不会被触发。 调用上述函数时,chrome dev 工具控制台中没有任何痕迹,它打开文件选择窗口,选择图像上传后,它只是返回,然后屏幕刷新。

更新 6/26经过更多测试后注意到两种不同的 android 设备之间的这种行为不同,一个是果冻豆版本 4.4.2,另一个是果冻豆版本 4.4.4。 在使用 4.4.4 navigator.camera.getPicture 运行的设备中被成功调用但在文件传输时被击中。 运行 Jelly bean 4.4.2 的设备刚刚在 navigator.camera.getPicture 失败,没有调用它的成功或错误回调函数,这是我几天以来遇到的问题。 看起来带有相机插件 2.2.0 的cordova 5.1 不适用于Jelly Bean 或其几个版本。 也许我需要找到适用于 Jelly Bean 的cordova 版本和cordova 相机插件版本。

更新 6/25将代码从 Telerik AppBuilder 迁移到 PhoneGap。 PhoneGap 使用 Cordova 5.1 并配置了最新的 Camera Plugin 2.2.0,但问题仍然相同。 不确定这个问题是否与这篇文章中的相同 很有趣 这个问题没有在发行说明中列出,我在不同的 android 版本中测试过它不起作用。 到目前为止,至少没有对 Telerik Premium 支持中提出的票做出回应。

更新 6/23 1:28IST正如Devid所建议的,按照我在调用 navigator.camera.getPicture 之前在推荐修复下方添加的这篇文章之后,问题仍然存在。

if (device.platform === 'Android') {
  setInterval(function () {
     cordova.exec(null, null, '', '', [])
  }, 200);
}

更新:6/23我从 chrome://inspect 检查了控制台中的行为,在文件上传活动期间绝对没有任何痕迹。 在网络选项卡中找不到此 http 请求。 为了了解它在哪里停止运行,我在uploadFile函数中的每个阶段添加了console.log,我注意到它没有在navigator.camera.getPicture中触发uploadPhoto回调函数,但是这个调用确实调用了文件选择器,但是在文件选择之后它的回调uploadPhoto函数没有触发。 该应用似乎在设备上没有某些访问权限。

navigator.camera.getPicture(
    uploadPhoto,
    function(message) {
        rst.innerHTML = "Failed to get a picture. Please select one.";
    }, {
        quality         : 50,
        destinationType : navigator.camera.DestinationType.FILE_URI,
        sourceType      : navigator.camera.PictureSourceType.PHOTOLIBRARY
    });

如果有任何线索,这里是亚行的目录。

更新 2/24-9:15AMIST下面是 Android 清单,我是否缺少任何权限?

<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="$AndroidVersionCode$"
          android:versionName="$BundleVersion$"
          package="$AppIdentifier$"
          android:windowSoftInputMode="adjustPan"
          android:hardwareAccelerated="$AndroidHardwareAcceleration$"
          xmlns:android="http://schemas.android.com/apk/res/android" >
    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
        />

    <application android:label="@string/app_name"
                 android:icon="@drawable/icon"
                 android:hardwareAccelerated="$AndroidHardwareAcceleration$"
                 android:debuggable="true" >
        <activity android:label="@string/app_name"
                  android:name=".TelerikCallbackActivity"
                  android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
                  android:launchMode="singleTop"
                  android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21"/>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

下面的cordova 文件上传代码在模拟器中运行完美,但在Android 设备中部署时失败。

下面是config.xml

<widget xmlns     = "http://www.w3.org/ns/widgets"
        version   = "2.0.0">

    <content src="index.html" />

    <!-- Whitelist docs: https://github.com/apache/cordova-plugin-whitelist -->

    <!-- allow local pages -->
    <!-- <access origin="http://127.0.0.1*"/> -->
    <access origin="*" />

    <!-- Grant certain URLs the ability to launch external applications. This
         behaviour is set to match that of Cordova versions before 3.6.0, and
         should be reviewed before launching an application in production. It
         may be changed in the future. -->
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-intent href="market:*" />

    <preference name="loglevel" value="DEBUG" />
    <!--
      <preference name="splashscreen" value="splash" />
      <preference name="backgroundColor" value="0xFFF" />
      <preference name="loadUrlTimeoutValue" value="20000" />
      <preference name="InAppBrowserStorageEnabled" value="true" />
      <preference name="disallowOverscroll" value="true" />
    -->
</widget>

下面是客户端代码

uploadFile: function () {
    rst = document.getElementById(this.id + 'res');
    rst.innerHTML = "";
    var uploadTYPE = this.id;
    navigator.camera.getPicture(
        uploadPhoto,
        function(message) {
            rst.innerHTML = "Failed to get a picture. Please select one.";
        }, {
            quality         : 50,
            destinationType : navigator.camera.DestinationType.FILE_URI,
            sourceType      : navigator.camera.PictureSourceType.PHOTOLIBRARY
        });

    function uploadPhoto(fileURI) {
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);

        if (cordova.platformId == "android") {
            options.fileName += ".jpg" 
        }

        options.mimeType = "image/jpeg";
        //options.httpMethod = "PUT";
        //options.contentType = 'multipart/form-data';
        var params = new Object();
        params.uid = localStorage.getItem("FOSCode");
        params.utyp = uploadTYPE;
        options.params = params; 

        options.headers = {
            Connection: "close"
        };
        //options.httpMethod = 'POST';
        options.chunkedMode = false;

        var ft = new FileTransfer();

        rst.innerHTML = "Upload in progress...";
        ft.upload(
            fileURI,
            encodeURI("https://www.kinrep.com/foster/upload.php"),
            onFileUploadSuccess,
            onFileTransferFail,
            options, true);

        function onFileUploadSuccess (result) {
           // rst.innerHTML = "Upload successful";
            console.log("FileTransfer.upload");
            console.log("Code = " + result.responseCode);
            console.log("Response = " + result.response);
            console.log("Sent = " + result.bytesSent);
            console.log("Link to uploaded file: https://www.kinrep.com/foster/ws/contentlibrary/" + result.response);
            var response = result.response;
            var destination = "https://www.kinrep.com/foster/WS/ContentLibrary/" + response.substr(response.lastIndexOf('=') + 1);
            if(this.id == 'uploadcheque') {
                document.getElementById("hdnchequeimgpath").value = destination;

            } else if(this.id == 'uploaddoorlock') {

                document.getElementById("hdndoorlockedimgpath").value = destination;
            } else {

                document.getElementById("hdnothersimgpath").value = destination;
            }
            rst.innerHTML = "File uploaded to: " +
                                                          destination + 
                                                          "</br><button class=\"button\" onclick=\"window.open('" + destination + "', '_blank', 'location=yes')\">Open Location</button>";
            //document.getElementById("downloadedImage").style.display="none";
        }

        function onFileTransferFail (error) {

            rst.innerHTML = "File Transfer failed: " + error.code;
            alert(rst.innerHTML);
            console.log("FileTransfer Error:");
            console.log("Code: " + error.code);
            console.log("Source: " + error.source);
            console.log("Target: " + error.target);
        }
    }

从 logcat 重现错误后,我无法从我的应用程序中跟踪任何内容。 Android 只是对我的应用程序的文件上传活动不做任何事情。 同样的活动在模拟器中完美运行。

好的,自从 12 天以来没有人回答我发布的问题,决定回答我自己的问题。 经过网络上有关此问题的各种调查结果。 我发现有一个社区正在测试认证跨设备的cordova 构建。 在他们的网站上,清楚地记录了 Android 设备的cordova-camera-plugin 存在问题。 以下是他们网站上提到的内容

科尔多瓦插件相机(版本 1.0.1)。 该相机未能通过我们在运行 Jellybean、Kitkat 和 Lollipop 的三星设备上的许多测试。 我们还注意到 iOS 和 Windows Phone 8 上的各种其他问题。 由于这些问题,我们不会将相机作为此套件的一部分。

我在问题中也提到了一些建议的解决方法,但不幸的是,这些方法在我现在正在测试的 Jelly Bean 平台上不起作用。

因此,合乎逻辑的结论是在 Android Studio 中使用 Android SDK 开发和构建本机应用程序,而不是浪费时间和精力寻找cordova-camera-plugin 的修复程序。 我知道这可能并不理想,但我认为这是目前的处理方式。 希望这个答案可以为一些遇到类似问题的人节省时间

看起来 Corodva 相机插件适用于最新的 android 6.x 版本。 https://issues.apache.org/jira/browse/CB-10857但这不适用于我的用户仍然使用旧 android 版本的情况。 当文件传输即使在本机 SDK 代码中也不起作用时,这将是唯一的选择,但这种情况不太可能发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM