cordova navigator.camera.getPicture不工作在Android [英] cordova navigator.camera.getPicture not working in android

查看:1283
本文介绍了cordova navigator.camera.getPicture不工作在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开发工具控制台没有跟踪它上面的函数被调用它打开文件选择窗口,并选择上传图像后,它只是返回,然后屏幕刷新。

In the above code uploadPhoto callback function never gets fired. And there is no traces of it in chrome dev tools console when the above function is called it opens the file selection window and after the image is selected for upload it simply returns, and then the screen gets refreshed.

更新6/26
更多的测试注意到这种行为在两个不同的Android设备之间有一个与Jelly bean版本4.4。 2和其他Jelly bean版本4.4.4。在使用4.4.4 navigator.camera.getPicture运行的设备已成功调用,但在文件传输时被触发。运行Jelly bean 4.4.2的设备刚刚失败在navigator.camera.getPicture没有调用它的成功或错误回调函数,这是我撞到了几天。看起来像cordova 5.1与相机插件2.2.0不工作与Jelly Bean或其几个版本。也许我需要找到cordova版本&在Jelly Bean上工作的cordova相机插件版本。

Update 6/26 After more testing noticed that this behavior different between two different android devices one with Jelly bean version 4.4.2 and other Jelly bean version 4.4.4. In the device running with 4.4.4 navigator.camera.getPicture was successfully called but struck at file transfer. The device running Jelly bean 4.4.2 has just failed at navigator.camera.getPicture without calling its success or error callback functions which is where I struck since few days. Looks like the cordova 5.1 with camera plugin 2.2.0 does not work with Jelly Bean or few of its versions. Maybe I will need to find cordova version & the cordova camera plugin version that works on Jelly Bean.

更新6/25
将代码从Telerik AppBuilder迁移到PhoneGap。 PhoneGap使用Cordova 5.1和配置最新的相机插件2.2.0,但问题仍然是相同的。不确定此问题是否与此 帖子 很有趣这个问题未列在 发行说明 ,我测试在不同的Android版本,它没有工作。

Update 6/25 Migrated the code from Telerik AppBuilder to PhoneGap. PhoneGap uses Cordova 5.1 and configured latest Camera Plugin 2.2.0, however issue is still the same. Not sure if this issue is same as in this post quite interesting this issue is not listed in release notes, I tested in different android versions it did not work. And atleast no response to the ticket raised in Telerik Premium support so far.

更新6/23 1:28IST
根据建议由Devid,跟随此信息我在下面添加了下面的建议修复,在调用navigator.camera.getPicture之前,问题仍然相同。

Update 6/23 1:28IST As suggested by Devid, followed this post I added below recommended fix right before the call navigator.camera.getPicture, the issue remains same.

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

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

UPDATE: 6/23 I checked the behavior in the console from chrome://inspect there is absolutely no trace during file upload activity. In the network tab could not find this http request. To understand where it stopped functioning I added console.log each stage within uploadFile function what I noticed was it did not trigger uploadPhoto callback function in navigator.camera.getPicture, but this call does invoked file chooser, but after file selection its callback uploadPhoto function did not trigger. Looks like the app is not having some access rights on the device.

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
    });

Here is the catlog from ADB if at all gives some clue.

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

Update 2/24-9:15AMIST Below is the Android manifest, is there any permission that I am missing?

<?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设备中部署失败。

The below cordova file upload code works perfect in simulator but fails when deployed in Android device.

以下是config.xml

Below is 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只是简单地不做任何事情从文件上传活动从我的应用程序。

From the logcat I could not trace anything from my app after reproducing the error. Android just simply does not do anything on file upload activity from my app. The same activity works perfect from simulator.

推荐答案

好的,因为没有人回答我的问题,自12天以来,决定回答我的自己的问题。在这个问题在网上的各种调查结果后。我发现有一个社区是测试认证cordova构建跨设备。在他们的网站上清楚地记录了,Android设备上有cordova-camera-plugin的问题。以下是其 网站 中提及的内容

Okay, since no one answered my question posted since 12 days, decided to answer my own question. After various findings on this issue across the net. I found there is one community which is test certifying cordova builds across the devices. In their website it is clearly documented that there are issues with cordova-camera-plugin with Android devices. Below is what is mentioned at their website

cordova-plugin-camera(1.0.1版)。相机失败了我们对运行Jellybean,Kitkat和Lollipop的三星设备的许多测试。我们注意到iOS和Windows Phone 8中的各种其他问题。由于这些问题,我们不会将相机作为此套件的一部分。

cordova-plugin-camera (version 1.0.1). The camera fails many of our tests on Samsung devices running Jellybean, Kitkat, and Lollipop. We noticed various other issues across iOS and Windows Phone 8 as well. Because of these issues, we will not be including the camera as a part of this kit.

有一些建议的解决方法我也在我的问题中提到,但不幸的是,那些没有工作的Jelly Bean平台我正在测试现在。

There are some suggested workarounds which I also referred in my question, but unfortunately those did not work on Jelly Bean platforms I am testing with right now.

因此,逻辑结论是开发& ;在Android工作室中使用Android SDK构建本机应用程序,而不是浪费时间&能量在寻找cordova摄像机插件的修复。我知道这可能不是理想的,但我认为这是如何处理在这个时间点。希望此回答可以为遇到类似问题的人节省时间

And hence so the logical conclusion is to develop & build the native app using Android SDK in Android studio instead wasting time & energy in finding a fix for cordova-camera-plugin. I know this may not be ideal but I think this is how to deal with at this point of time. Hope this answer saves time to some who experience similar issues

看起来像corodva相机插件与最新的Android 6.x版本。 https://issues.apache.org/jira/browse/CB-10857 但这不会工作,我的情况下,我的用户仍然与旧的Android版本。这将是文件传输无法工作,即使在本机SDK代码,但这是不可能的情况下的唯一选择。

Looks like corodva camera plugin works with latest android 6.x versions. https://issues.apache.org/jira/browse/CB-10857 But this will not work for my case where my users are still with old android versions. This would be the only choice when file transfers wont work even in native SDK code, but that is quite unlikely the case.

这篇关于cordova navigator.camera.getPicture不工作在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆