Cordoba navigator.camera.getPicture not working in android

navigator.camera.getPicure function does not work. Its callback function never starts in the code below. This feature is for cordova camera plugins.

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

In the above code, the uploadPhoto callback function never starts. And there are no traces of this in the Chrome Chrome console, when this function is called, it opens a file selection window and after the image is selected for download, it simply returns, and then the screen refreshes.

Update 6/26 After more testing, we noticed that this behavior is different between two different Android devices with Jelly bean version 4.4.2 and another jelly bean version 4.4.4. In a device working with 4.4.4, navigator.camera.getPicture was successfully called, but hit the file transfer. The device running Jelly bean 4.4.2 just failed in navigator.camera.getPicture without calling it a success or error callback function, where I hit from a few days later. It seems that cordova 5.1 with the camera plug-in 2.2.0 does not work with jelly bean or several versions of it. I may need to find the cordova version and the plugin version for the cordova camera that runs on Bean jelly.

Update 6/25 Migrated code from Telerik AppBuilder to PhoneGap. PhoneGap uses Cordova 5.1 and configured the latest version of Camera Plugin 2.2.0, but the problem is the same. Not sure if this problem is the same as this post is quite interesting, this problem is not listed in the release notes , I tested in different versions of Android, it did not work. And at least the answer to the ticket raised in support of Telerik Premium so far.

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

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

UPDATE: 6/23 I checked the behavior in the console from chrome: // make sure that there is no trace when downloading files. On the network tab, this HTTP request could not be found. To understand where it stopped working, I added console.log each step in the uploadFile function, which, as I noticed, did not call the uploadPhoto callback function in navigator.camera.getPicture, but this call caused the file to be selected, but after selecting the file it uploadPhoto callback function did not work. It seems that the application does not have access rights to 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 key.

Update 2 / 24-9: 15AMIST Below is the Android manifest, are there any permissions 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> 

Below, the root file upload code works fine in the simulator, but does not work when deployed to an Android device.

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> 

Below is the customer code

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

From logcat, I was not able to trace anything from my application after reproducing the error. Android just does nothing of file upload activity from my application. The same action works great with a simulator.

+3
source share
1 answer

Well, since no one answered my question published from 12 days, I decided to answer my own question. After various conclusions on this subject online. I found that there is one community that tests cordova on all devices. On their website, it is clearly documented that there are problems with the cordova-camera-plugin with Android devices. Below is what is mentioned on the 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.

There are some suggested workarounds that I also mentioned in my question, but unfortunately they did not work on the Jelly Bean platforms that I'm testing right now.

And therefore, the logical conclusion is to develop and create your own application using the Android SDK in the Android studio, instead spending time and energy looking for a fix for the cordon-camera plugin. I know that this may not be ideal, but I think that this can be dealt with at this point in time. Hope this answer saves time for those experiencing similar problems.

It looks like the Corodva Camera plugin works with the latest versions of Android 6.x. https://issues.apache.org/jira/browse/CB-10857 But this will not work for my case, when my users are still with older versions of Android. This would be the only choice when file transfer will not work even in its own SDK code, but this is hardly possible.

+1
source

Source: https://habr.com/ru/post/969685/


All Articles