2016-06-14 36 views
3

navigator.camera.getPicure函数不工作。它的回调函数在下面的代码中不会被触发。该功能来自cordova相机插件。cordova navigator.camera.getPicture不能在android中工作

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设备一个Jelly Bean系统版本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相机插件版本。

更新6/25 将代码从Telerik AppBuilder迁移到PhoneGap。 PhoneGap使用Cordova 5.1并配置了最新的Camera Plugin 2.2.0,但问题仍然相同。不知道这个问题是否与此post相同有趣这个问题没有在release notes中列出,我在不同的android版本测试它不工作。到目前为止,Telerik Premium支持中至少没有回应票。

更新6/23 1:28IST 作为建议的DEVID,其次this post我在下面推荐的修复程序的调用navigator.camera.getPicture前右,问题仍然是相同的。

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

UPDATE:6/23 我检查的行为,从镀铬的控制台://检查是绝对文件上传活动期间无痕。在网络标签找不到这个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 
    }); 

Here是亚行如果目录下载都给出了一些线索。

更新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> 

下面科尔多瓦文件上传代码工作完美的模拟器,但在Android设备部署时失败。

以下是配置。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只是简单地不会对我的应用上的文件上传活动做任何事情。相同的活动从模拟器完美工作。

+0

您是否可以先从您的手机浏览器访问此链接 - “https://www.kinrep.com”? – Gandhi

+0

是的。该网站可以从设备上通过浏览器访问 – Naga

+0

您是否尝试过使用Chrome devtools解除它?如果不是,你可以在这里发布结果,以便我们可以帮助你。 这是做这个的指南https://developer.chrome.com/devtools/docs/remote-debugging –

回答

1

好吧,既然没有人回答我自12天以来发布的问题,决定回答我自己的问题。通过网络在这个问题上的各种调查结果。我发现有一个社区是测试认证cordova在各种设备上构建的。在他们的网站中,明确记录了使用Android设备的cordova-camera-plugin存在问题。下面是什么,是在提到website

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.

有一些建议的解决办法,我也是在我的问题提到,但不幸的是那些没有对果冻豆平台,我现在用的测试工作。

因此,合乎逻辑的结论是开发&在Android Studio中使用Android SDK构建原生应用程序,而不是浪费时间&找到cordova-camera-plugin的修复程序。我知道这可能不是理想的,但我认为这是如何处理在这个时间点。希望这个答案可以为遇到类似问题的人节省时间

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

相关问题